<?php
require_once dirname(__FILE__) . '/../includes/common.inc.php';
require_once dirname(__FILE__) . "/../database/$database/database.inc.php";
  
header('Content-Type: text/plain');

global $myManager;

$action = MiscUtils::getParam('action', NULL);

$myManager->start_t();
try {
  switch ($action) {
    case 'login':
      login($myManager);
      break;
    case 'addCustomer':
      addCustomer($myManager);
      break;
    case 'updateCustomer':
      updateCustomer($myManager);
      break;
    case 'customers':
      customers($myManager);
      break;
    case 'payments':
      payments($myManager);
      break;
    case 'expenses':
      expenses($myManager);
      break;
    case 'ieincomecustomerall':
      ieincomecustomerall($myManager);
      break;
    case 'ieincomecustomer':
      ieincomecustomer($myManager);
      break;
    case 'ieincomedaily':
      ieincomedaily($myManager);
      break;
    case 'ieexpense':
      ieexpense($myManager);
      break;
    case 'iesumall':
      iesumall($myManager);
      break;
    case 'iesumincome':
      iesumincome($myManager);
      break;
    case 'iesumexpense':
      iesumexpense($myManager);
      break;
    case 'iestatspackage':
      iestatspackage($myManager);
      break;
    case 'iestatsproduct':
      iestatsproduct($myManager);
      break;
    case 'iestatssource':
      iestatssource($myManager);
      break;
    case 'iestatsculture':
      iestatsculture($myManager);
      break;
    case 'iestatsreception':
      iestatsreception($myManager);
      break;
    case 'iestatsreceptioncustomers':
      iestatsreceptioncustomers($myManager);
      break;
    case 'iestatsceremony':
      iestatsceremony($myManager);
      break;
    case 'iestatsceremonycustomers':
      iestatsceremonycustomers($myManager);
      break;
    case 'iecompbymonth':
      iecompbymonth($myManager);
      break;
    case 'iecompbyyear':
      iecompbyyear($myManager);
      break;
    case 'iesumexpensedetail':
      iesumexpensedetail($myManager);
      break;
    case 'acctexpenses':
      acctexpenses($myManager);
      break;
    case 'acctexpense':
      acctexpense($myManager);
      break;
    case 'acctpayments':
      acctpayments($myManager);
      break;
    case 'acctpayment':
      acctpayment($myManager);
      break;
    case 'asstwages':
      asstwages($myManager);
      break;
    case 'weeklywage':
      weeklywage($myManager);
      break;
     case 'weeklywage1':
      weeklywage1($myManager);
      break;
    case 'packagesample':
      packagesample($myManager);
      break;
    case 'purchasesample':
      purchasesample($myManager);
      break;
    case 'packagepurchase':
      packagepurchase($myManager);
      break;
    default:
      break;
  }
} catch (PDOException $e) {
  echo $e->getMessage();
}
$myManager->commit_t();

function _months($from, $to) {
  $result = array();
  for ($i = $from->year, $il = $to->year; $i <= $il; $i++) {
    for ($j = 1, $jl = 12; $j <= $jl; $j++) {
      if ($i == $from->year && $j < $from->month) {
        continue;
      } else if ($i == $to->year && $j > $to->month) {
        continue;
      }
      $tmp = new stdClass();
      $tmp->month = $j;
      $tmp->year = $i;
      $result[] = $tmp;
    }
  }
  return $result;
}

function _days($year, $month) {
  if ($month == 1) {
    return 31;
  } else if ($month == 2) {
    if ($year % 4 == 0) {
      return 29;
    } else {
      return 28;
    }
  } else if ($month == 3) {
    return 31;
  } else if ($month == 4) {
    return 30;
  } else if ($month == 5) {
    return 31;
  } else if ($month == 6) {
    return 30;
  } else if ($month == 7) {
    return 31;
  } else if ($month == 8) {
    return 31;
  } else if ($month == 9) {
    return 30;
  } else if ($month == 10) {
    return 31;
  } else if ($month == 11) {
    return 30;
  } else if ($month == 12) {
    return 31;
  }
}

function login($myManager) {
  $orm = classToOrm('user');
  $account = MiscUtils::getParam('a', NULL);
  $password = MiscUtils::getParam('p', START);

  $result = $orm->find($myManager, NULL, NULL, NULL, NULL, "d.account = '$account' AND d.password = '$password'", NULL);
  if (count($result->data) == 1) {
    $user = $result->data[0];
    $_SESSION[OPERATOR] = $user;
    
    setcookie(OPERATOR, json_encode($user), (time() + 3600), PATH, DOMAIN);
    
    echo json_encode($user);
  } else {
    echo json_encode(array());
  }
}

function addCustomer($myManager) {
  $orm = classToOrm('customer');
  $customer = json_decode(MiscUtils::getParam('m', NULL));
  
  $epCustomer = $orm->add($myManager, $customer);
  $myManager->commit_t();
  $myManager->start_t();
  $customer = $orm->__toObject($myManager, $epCustomer, new stdClass());
  
  echo json_encode($customer);
}

function updateCustomer($myManager) {
  $orm = classToOrm('customer');
  $customer = json_decode(MiscUtils::getParam('m', NULL));
  
  $epCustomer = $orm->update($myManager, $customer);
  $myManager->commit_t();
  $myManager->start_t();
  $customer = $orm->__toObject($myManager, $epCustomer, new stdClass());
  
  echo json_encode($customer);
}

function customers($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);
  
  $date = MiscUtils::getParam('date', NULL);
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $condition .= ($date) ? ' AND (d.orders.contains(o) AND o.date = \'' . SimpleDate::toStamp(json_decode($date)) . '\')' : '';
  $condition .= ($createdFrom) ? ' AND (d.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $orm = classToOrm('customer');
  if ($orm) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      echo json_encode($result);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}
/*-------------zjx----------------*/
function payments($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', 'd.date');
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $condition .= ($createdFrom) ? ' AND (d.date >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.date <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $orm = classToOrm('payment');
  if ($orm) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      echo json_encode($result);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function expenses($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $condition .= ($createdFrom) ? ' AND (d.payDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.payDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $orm = classToOrm('expense');
  if ($orm) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      echo json_encode($result);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}
/*---------------------------------------*/
function ieincomecustomerall($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $condition .= ($createdFrom) ? ' AND (d.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $orm = classToOrm('customer');
  if ($orm) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      
      $total = 0; $received = 0; $cash = 0; $rev = 0; $c = 0; $nc = 0;
      for($i = 0, $il = count($result->data); $i < $il; $i++) {
        $customer = $result->data[$i];
        for ($j = 0, $jl = count($customer->orders); $j < $jl; $j++) {
          $order = $customer->orders[$j];
          if ($order->disposal->status) {
            continue;
          }
          if ($j == $jl-1) {
            $c++;
          }
          $total += $order->price;
          
          if (count($order->payments) == 0) {
            $nc++;
            $rev += $order->price;
          }
          for ($k = 0, $kl = count($order->payments); $k < $kl; $k++) {
            $payment = $order->payments[$k];
            if ($payment->cancelled) {
              continue;
            }
            $received += $payment->amount;
            if ($payment->method == 'cash') {
              $cash += $payment->amount;
            }
          }
        }
      }
      $sum = new stdClass();
      $sum->customer = $c;
      $sum->tolRev = $total;
      $sum->receipt = $received;
      $sum->anticipated = $total - $received;
      $sum->cash = $cash;
      $sum->noncash = $received - $cash;
      $sum->noPayment = $nc;
      $sum->revenue = $rev;
      $sum->netrev = $total - $rev;
      echo json_encode($sum);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function ieincomecustomer($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $condition .= ($createdFrom) ? ' AND (d.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $orm = classToOrm('customer');
  if ($orm) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      
      $cus = 0; $wedrev = 0; $wedrec = 0; $wedcash = 0; $wednonc = 0;
      $prerev = 0; $prerec = 0; $precash = 0; $prenonc = 0;
      $porrev = 0; $porrec = 0; $porcash = 0; $pornonc = 0;
      $engrev = 0; $engrec = 0; $engcash = 0; $engnonc = 0;
      $othrev = 0; $othrec = 0; $othcash = 0; $othnonc = 0;
      
      for($i = 0, $il = count($result->data); $i < $il; $i++) {
        $customer = $result->data[$i];
        $cus++;
        for ($j = 0, $jl = count($customer->orders); $j < $jl; $j++) {
          $order = $customer->orders[$j];
          if ($order->disposal->status) {
            continue;
          }
          if ($order->type == ORDER_TYPE_WEDDING) {
            $wedrev += $order->price;
            for ($k = 0, $kl = count($order->payments); $k < $kl; $k++) {
              $payment = $order->payments[$k];
              if ($payment->cancelled) {
                continue;
              }
              $wedrec += $payment->amount;
              if ($payment->method == 'cash') {
                $wedcash += $payment->amount;
              } else {
                $wednonc += $payment->amount;
              }
            }
          } else if ($order->type == ORDER_TYPE_PREWEDDING) {
            $prerev += $order->price;
            for ($k = 0, $kl = count($order->payments); $k < $kl; $k++) {
              $payment = $order->payments[$k];
              if ($payment->cancelled) {
                continue;
              }
              $prerec += $payment->amount;
              if ($payment->method == 'cash') {
                $precash += $payment->amount;
              } else {
                $prenonc += $payment->amount;
              }
            }
          } else if ($order->type == ORDER_TYPE_PORTRAIT) {
            $porrev += $order->price;
            for ($k = 0, $kl = count($order->payments); $k < $kl; $k++) {
              $payment = $order->payments[$k];
              if ($payment->cancelled) {
                continue;
              }
              $porrec += $payment->amount;
              if ($payment->method == 'cash') {
                $porcash += $payment->amount;
              } else {
                $pornonc += $payment->amount;
              }
            }
          } else if ($order->type == ORDER_TYPE_ENGAGEMENT) {
            $engrev += $order->price;
            for ($k = 0, $kl = count($order->payments); $k < $kl; $k++) {
              $payment = $order->payments[$k];
              if ($payment->cancelled) {
                continue;
              }
              $engrec += $payment->amount;
              if ($payment->method == 'cash') {
                $engcash += $payment->amount;
              } else {
                $engnonc += $payment->amount;
              }
            }
          } else {
            $othrev += $order->price;
            for ($k = 0, $kl = count($order->payments); $k < $kl; $k++) {
              $payment = $order->payments[$k];
              if ($payment->cancelled) {
                continue;
              }
              $othrec += $payment->amount;
              if ($payment->method == 'cash') {
                $othcash += $payment->amount;
              } else {
                $othnonc += $payment->amount;
              }
            }
          }
        }
      }
      $wedsum = new stdClass();
      $wedsum->customer = $cus;
      $wedsum->revenue = $wedrev;
      $wedsum->receipt = $wedrec;
      $wedsum->anticipated = $wedrev - $wedrec;
      $wedsum->cash = $wedcash;
      $wedsum->noncash = $wednonc;
      $presum = new stdClass();
      $presum->customer = $cus;
      $presum->revenue = $prerev;
      $presum->receipt = $prerec;
      $presum->anticipated = $prerev - $prerec;
      $presum->cash = $precash;
      $presum->noncash = $prenonc;
      $porsum = new stdClass();
      $porsum->customer = $cus;
      $porsum->revenue = $porrev;
      $porsum->receipt = $porrec;
      $porsum->anticipated = $porrev - $porrec;
      $porsum->cash = $porcash;
      $porsum->noncash = $pornonc;
      $engsum = new stdClass();
      $engsum->customer = $cus;
      $engsum->revenue = $engrev;
      $engsum->receipt = $engrec;
      $engsum->anticipated = $engrev - $engrec;
      $engsum->cash = $engcash;
      $engsum->noncash = $engnonc;
      $othsum = new stdClass();
      $othsum->customer = $cus;
      $othsum->revenue = $othrev;
      $othsum->receipt = $othrec;
      $othsum->anticipated = $othrev - $othrec;
      $othsum->cash = $othcash;
      $othsum->noncash = $othnonc;
      
      $res = new stdClass();
      $res->wed = $wedsum;
      $res->pre = $presum;
      $res->por = $porsum;
      $res->eng = $engsum;
      $res->oth = $othsum;
      echo json_encode($res);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function ieincomedaily($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $condition .= ($createdFrom) ? ' AND (d.date >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.date <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $orm = classToOrm('payment');
  if ($orm) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      $receipt = 0; $cash = 0; $noncash = 0;
      for ($i = 0, $il = count($result->data); $i < $il; $i++) {
        $payment = $result->data[$i];
        if ($payment->cancelled) {
          continue;
        }
        $receipt += $payment->amount;
        if ($payment->method == 'cash') {
          $cash += $payment->amount;
        } else {
          $noncash += $payment->amount;
        }
      }
      $top = new stdClass();
      $top->receipt = $receipt;
      $top->cash = $cash;
      $top->noncash = $noncash;
      
      $res = new stdClass();
      $res->top = $top;
      echo json_encode($res);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function ieexpense($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);
  
  $orm = classToOrm('expense');
  if ($orm) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $exp = 0; $cash = 0; $noncash = 0;

      for ($i = 0, $il = count($result->data); $i < $il; $i++) {
        $expense = $result->data[$i];
        if ($expense->status == -1) {
          continue;
        }
        $exp += $expense->amount;
        if ($expense->method == 'C') {
          $cash += $expense->amount;
        } else {
          $noncash += $expense->amount;
        }
      }
      $res = new stdClass();
      $res->expense = $exp;
      $res->cash = $cash;
      $res->noncash = $noncash;

      echo json_encode($res);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function iesumall($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $condition1 = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $condition .= ($createdFrom) ? ' AND (d.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $condition1 .= ($createdFrom) ? ' AND (d.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition1 .= ($createdTo) ? ' AND (d.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  
  $orm = classToOrm('payment');
  $orm1 = classToOrm('expense');
  if ($orm && $orm1) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      $result1 = $orm1->find($myManager, $page, $size, $order, $queue, $condition1, $function);
      $result1->total = count($orm1->find($myManager, START, INFINITE, NULL, NULL, $condition1, NULL)->data);
      $now = SimpleDate::create();
      $il = intval($now->month);
      $jl = (13 - intval($now->month));
      $mon = array();
      $yea = array();
  
      for ($j = 0; $j < $jl; $j++) {
        $m = $j + $il;
        $y = intval($now->year) - 1;
        array_push($mon, $m);
        array_push($yea, $y);
      }
      for ($i = 1; $i <= $il; $i++) {
        $m2 = $i;
        $y2 = intval($now->year);
        array_push($mon, $m2);
        array_push($yea, $y2);
      }
      $rec1 = 0; $rec2 = 0; $rec3 = 0; $rec4 = 0; $rec5 = 0; $rec6 = 0; $rec7 = 0; $rec8 = 0; $rec9 = 0; $rec10 = 0; $rec11 = 0; $rec12 = 0; $rec13 = 0;
      $rcash1 = 0; $rcash2 = 0; $rcash3 = 0; $rcash4 = 0; $rcash5 = 0; $rcash6 = 0; $rcash7 = 0; $rcash8 = 0; $rcash9 = 0; $rcash10 = 0; $rcash11 = 0; $rcash12 = 0; $rcash13 = 0;
      $ecash1 = 0; $ecash2 = 0; $ecash3 = 0; $ecash4 = 0; $ecash5 = 0; $ecash6 = 0; $ecash7 = 0; $ecash8 = 0; $ecash9 = 0; $ecash10 = 0; $ecash11 = 0; $ecash12 = 0; $ecash13 = 0;
      $enoncash1 = 0; $enoncash2 = 0; $enoncash3 = 0; $enoncash4 = 0; $enoncash5 = 0; $enoncash6 = 0; $enoncash7 = 0; $enoncash8 = 0; $enoncash9 = 0; $enoncash10 = 0; $enoncash11 = 0; $enoncash12 = 0; $enoncash13 = 0;

      for ($i = 0, $il = count($result->data); $i < $il; $i++) {
        $payment = $result->data[$i];
        if ($payment->cancelled) {
          continue;
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[0], $mon[0], 1)) <= SimpleDate::toStamp($payment->date)) && (SimpleDate::toStamp($payment->date) <= SimpleDate::toStamp(SimpleDate::create($yea[0], $mon[0], 31)))) {
          $rec1 += $payment->amount;
          if ($payment->method == 'cash') {
            $rcash1 += $payment->amount;
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[1], $mon[1], 1)) <= SimpleDate::toStamp($payment->date)) && (SimpleDate::toStamp($payment->date) <= SimpleDate::toStamp(SimpleDate::create($yea[1], $mon[1], 31)))) {
          $rec2 += $payment->amount;
          if ($payment->method == 'cash') {
            $rcash2 += $payment->amount;
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[2], $mon[2], 1)) <= SimpleDate::toStamp($payment->date)) && (SimpleDate::toStamp($payment->date) <= SimpleDate::toStamp(SimpleDate::create($yea[2], $mon[2], 31)))) {
          $rec3 += $payment->amount;
          if ($payment->method == 'cash') {
            $rcash3 += $payment->amount;
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[3], $mon[3], 1)) <= SimpleDate::toStamp($payment->date)) && (SimpleDate::toStamp($payment->date) <= SimpleDate::toStamp(SimpleDate::create($yea[3], $mon[3], 31)))) {
          $rec4 += $payment->amount;
          if ($payment->method == 'cash') {
            $rcash4 += $payment->amount;
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[4], $mon[4], 1)) <= SimpleDate::toStamp($payment->date)) && (SimpleDate::toStamp($payment->date) <= SimpleDate::toStamp(SimpleDate::create($yea[4], $mon[4], 31)))) {
          $rec5 += $payment->amount;
          if ($payment->method == 'cash') {
            $rcash5 += $payment->amount;
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[5], $mon[5], 1)) <= SimpleDate::toStamp($payment->date)) && (SimpleDate::toStamp($payment->date) <= SimpleDate::toStamp(SimpleDate::create($yea[5], $mon[5], 31)))) {
          $rec6 += $payment->amount;
          if ($payment->method == 'cash') {
            $rcash6 += $payment->amount;
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[6], $mon[6], 1)) <= SimpleDate::toStamp($payment->date)) && (SimpleDate::toStamp($payment->date) <= SimpleDate::toStamp(SimpleDate::create($yea[6], $mon[6], 31)))) {
          $rec7 += $payment->amount;
          if ($payment->method == 'cash') {
            $rcash7 += $payment->amount;
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[7], $mon[7], 1)) <= SimpleDate::toStamp($payment->date)) && (SimpleDate::toStamp($payment->date) <= SimpleDate::toStamp(SimpleDate::create($yea[7], $mon[7], 31)))) {
          $rec8 += $payment->amount;
          if ($payment->method == 'cash') {
            $rcash8 += $payment->amount;
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[8], $mon[8], 1)) <= SimpleDate::toStamp($payment->date)) && (SimpleDate::toStamp($payment->date) <= SimpleDate::toStamp(SimpleDate::create($yea[8], $mon[8], 31)))) {
          $rec9 += $payment->amount;
          if ($payment->method == 'cash') {
            $rcash9 += $payment->amount;
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[9], $mon[9], 1)) <= SimpleDate::toStamp($payment->date)) && (SimpleDate::toStamp($payment->date) <= SimpleDate::toStamp(SimpleDate::create($yea[9], $mon[9], 31)))) {
          $rec10 += $payment->amount;
          if ($payment->method == 'cash') {
            $rcash10 += $payment->amount;
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[10], $mon[10], 1)) <= SimpleDate::toStamp($payment->date)) && (SimpleDate::toStamp($payment->date) <= SimpleDate::toStamp(SimpleDate::create($yea[10], $mon[10], 31)))) {
          $rec11 += $payment->amount;
          if ($payment->method == 'cash') {
            $rcash11 += $payment->amount;
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[11], $mon[11], 1)) <= SimpleDate::toStamp($payment->date)) && (SimpleDate::toStamp($payment->date) <= SimpleDate::toStamp(SimpleDate::create($yea[11], $mon[11], 31)))) {
          $rec12 += $payment->amount;
          if ($payment->method == 'cash') {
            $rcash12 += $payment->amount;
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[12], $mon[12], 1)) <= SimpleDate::toStamp($payment->date)) && (SimpleDate::toStamp($payment->date) <= SimpleDate::toStamp(SimpleDate::create($yea[12], $mon[12], 31)))) {
          $rec13 += $payment->amount;
          if ($payment->method == 'cash') {
            $rcash13 += $payment->amount;
          }
        }
      }
      for ($j = 0, $jl = count($result1->data); $j < $jl; $j++) {
        $expense = $result1->data[$j];
        if ((SimpleDate::toStamp(SimpleDate::create($yea[0], $mon[0], 1)) <= SimpleDate::toStamp($expense->payDate)) && (SimpleDate::toStamp($expense->payDate) <= SimpleDate::toStamp(SimpleDate::create($yea[0], $mon[0], 31)))) {
          if ($expense->method == 'C') {
            $ecash1 += $expense->amount;
          } else {
            $enoncash1 += $expense->amount;
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[1], $mon[1], 1)) <= SimpleDate::toStamp($expense->payDate)) && (SimpleDate::toStamp($expense->payDate) <= SimpleDate::toStamp(SimpleDate::create($yea[1], $mon[1], 31)))) {
          if ($expense->method == 'C') {
            $ecash2 += $expense->amount;
          } else {
            $enoncash2 += $expense->amount;
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[2], $mon[2], 1)) <= SimpleDate::toStamp($expense->payDate)) && (SimpleDate::toStamp($expense->payDate) <= SimpleDate::toStamp(SimpleDate::create($yea[2], $mon[2], 31)))) {
          if ($expense->method == 'C') {
            $ecash3 += $expense->amount;
          } else {
            $enoncash3 += $expense->amount;
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[3], $mon[3], 1)) <= SimpleDate::toStamp($expense->payDate)) && (SimpleDate::toStamp($expense->payDate) <= SimpleDate::toStamp(SimpleDate::create($yea[3], $mon[3], 31)))) {
          if ($expense->method == 'C') {
            $ecash4 += $expense->amount;
          } else {
            $enoncash4 += $expense->amount;
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[4], $mon[4], 1)) <= SimpleDate::toStamp($expense->payDate)) && (SimpleDate::toStamp($expense->payDate) <= SimpleDate::toStamp(SimpleDate::create($yea[4], $mon[4], 31)))) {
          if ($expense->method == 'C') {
            $ecash5 += $expense->amount;
          } else {
            $enoncash5 += $expense->amount;
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[5], $mon[5], 1)) <= SimpleDate::toStamp($expense->payDate)) && (SimpleDate::toStamp($expense->payDate) <= SimpleDate::toStamp(SimpleDate::create($yea[5], $mon[5], 31)))) {
          if ($expense->method == 'C') {
            $ecash6 += $expense->amount;
          } else {
            $enoncash6 += $expense->amount;
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[6], $mon[6], 1)) <= SimpleDate::toStamp($expense->payDate)) && (SimpleDate::toStamp($expense->payDate) <= SimpleDate::toStamp(SimpleDate::create($yea[6], $mon[6], 31)))) {
          if ($expense->method == 'C') {
            $ecash7 += $expense->amount;
          } else {
            $enoncash7 += $expense->amount;
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[7], $mon[7], 1)) <= SimpleDate::toStamp($expense->payDate)) && (SimpleDate::toStamp($expense->payDate) <= SimpleDate::toStamp(SimpleDate::create($yea[7], $mon[7], 31)))) {
          if ($expense->method == 'C') {
            $ecash8 += $expense->amount;
          } else {
            $enoncash8 += $expense->amount;
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[8], $mon[8], 1)) <= SimpleDate::toStamp($expense->payDate)) && (SimpleDate::toStamp($expense->payDate) <= SimpleDate::toStamp(SimpleDate::create($yea[8], $mon[8], 31)))) {
          if ($expense->method == 'C') {
            $ecash9 += $expense->amount;
          } else {
            $enoncash9 += $expense->amount;
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[9], $mon[9], 1)) <= SimpleDate::toStamp($expense->payDate)) && (SimpleDate::toStamp($expense->payDate) <= SimpleDate::toStamp(SimpleDate::create($yea[9], $mon[9], 31)))) {
          if ($expense->method == 'C') {
            $ecash10 += $expense->amount;
          } else {
            $enoncash10 += $expense->amount;
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[10], $mon[10], 1)) <= SimpleDate::toStamp($expense->payDate)) && (SimpleDate::toStamp($expense->payDate) <= SimpleDate::toStamp(SimpleDate::create($yea[10], $mon[10], 31)))) {
          if ($expense->method == 'C') {
            $ecash11 += $expense->amount;
          } else {
            $enoncash11 += $expense->amount;
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[11], $mon[11], 1)) <= SimpleDate::toStamp($expense->payDate)) && (SimpleDate::toStamp($expense->payDate) <= SimpleDate::toStamp(SimpleDate::create($yea[11], $mon[11], 31)))) {
          if ($expense->method == 'cash') {
            $ecash12 += $expense->amount;
          } else {
            $enoncash12 += $expense->amount;
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[12], $mon[12], 1)) <= SimpleDate::toStamp($expense->payDate)) && (SimpleDate::toStamp($expense->payDate) <= SimpleDate::toStamp(SimpleDate::create($yea[12], $mon[12], 31)))) {
          if ($expense->method == 'C') {
            $ecash13 += $expense->amount;
          } else {
            $enoncash13 += $expense->amount;
          }
        }
      }
      $m1 = new stdClass(); $m2 = new stdClass(); $m3 = new stdClass(); $m4 = new stdClass(); $m5 = new stdClass(); $m6 = new stdClass();
      $m7 = new stdClass(); $m8 = new stdClass(); $m9 = new stdClass(); $m10 = new stdClass(); $m11 = new stdClass(); $m12 = new stdClass();
      $m13 = new stdClass();
      $m1->month = $mon[0];
      $m1->year = $yea[0];
      $m1->receipt = $rec1;
      $m1->rcash = $rcash1;
      $m1->ecash = $ecash1;
      $m1->enoncash = $enoncash1;
      
      $m2->month = $mon[1];
      $m2->year = $yea[1];
      $m2->receipt = $rec2;
      $m2->rcash = $rcash2;
      $m2->ecash = $ecash2;
      $m2->enoncash = $enoncash2;
      
      $m3->month = $mon[2];
      $m3->year = $yea[2];
      $m3->receipt = $rec3;
      $m3->rcash = $rcash3;
      $m3->ecash = $ecash3;
      $m3->enoncash = $enoncash3;
      
      $m4->month = $mon[3];
      $m4->year = $yea[3];
      $m4->receipt = $rec4;
      $m4->rcash = $rcash4;
      $m4->ecash = $ecash4;
      $m4->enoncash = $enoncash4;
      
      $m5->month = $mon[4];
      $m5->year = $yea[4];
      $m5->receipt = $rec5;
      $m5->rcash = $rcash5;
      $m5->ecash = $ecash5;
      $m5->enoncash = $enoncash5;
      
      $m6->month = $mon[5];
      $m6->year = $yea[5];
      $m6->receipt = $rec6;
      $m6->rcash = $rcash6;
      $m6->ecash = $ecash6;
      $m6->enoncash = $enoncash6;
      
      $m7->month = $mon[6];
      $m7->year = $yea[6];
      $m7->receipt = $rec7;
      $m7->rcash = $rcash7;
      $m7->ecash = $ecash7;
      $m7->enoncash = $enoncash7;
      
      $m8->month = $mon[7];
      $m8->year = $yea[7];
      $m8->receipt = $rec8;
      $m8->rcash = $rcash8;
      $m8->ecash = $ecash8;
      $m8->enoncash = $enoncash8;
      
      $m9->month = $mon[8];
      $m9->year = $yea[8];
      $m9->receipt = $rec9;
      $m9->rcash = $rcash9;
      $m9->ecash = $ecash9;
      $m9->enoncash = $enoncash9;
      
      $m10->month = $mon[9];
      $m10->year = $yea[9];
      $m10->receipt = $rec10;
      $m10->rcash = $rcash10;
      $m10->ecash = $ecash10;
      $m10->enoncash = $enoncash10;
      
      $m11->month = $mon[10];
      $m11->year = $yea[10];
      $m11->receipt = $rec11;
      $m11->rcash = $rcash11;
      $m11->ecash = $ecash11;
      $m11->enoncash = $enoncash11;
      
      $m12->month = $mon[11];
      $m12->year = $yea[11];
      $m12->receipt = $rec12;
      $m12->rcash = $rcash12;
      $m12->ecash = $ecash12;
      $m12->enoncash = $enoncash12;
      
      $m13->month = $mon[12];
      $m13->year = $yea[12];
      $m13->receipt = $rec13;
      $m13->rcash = $rcash13;
      $m13->ecash = $ecash13;
      $m13->enoncash = $enoncash13;
      
      $res = array();
      array_push($res, $m1, $m2, $m3, $m4, $m5, $m6, $m7, $m8, $m9, $m10, $m11, $m12, $m13);
      echo json_encode($res);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function iesumincome($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);

  $condition .= ($createdFrom) ? ' AND (d.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $orm = classToOrm('customer');
  if ($orm) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      $now = SimpleDate::create();
      $il = intval($now->month);
      $jl = (13 - intval($now->month));
      $mon = array();
      $yea = array();
  
      for ($j = 0; $j < $jl; $j++) {
        $m = $j + $il;
        $y = intval($now->year) - 1;
        array_push($mon, $m);
        array_push($yea, $y);
      }
      for ($i = 1; $i <= $il; $i++) {
        $m2 = $i;
        $y2 = intval($now->year);
        array_push($mon, $m2);
        array_push($yea, $y2);
      }
      $wedcus1 = 0; $wedcus2 = 0; $wedcus3 = 0; $wedcus4 = 0; $wedcus5 = 0; $wedcus6 = 0; $wedcus7 = 0; $wedcus8 = 0; $wedcus9 = 0; $wedcus10 = 0; $wedcus11 = 0; $wedcus12 = 0; $wedcus13 = 0;
      $wedtol1 = 0; $wedtol2 = 0; $wedtol3 = 0; $wedtol4 = 0; $wedtol5 = 0; $wedtol6 = 0; $wedtol7 = 0; $wedtol8 = 0; $wedtol9 = 0; $wedtol10 = 0; $wedtol11 = 0; $wedtol12 = 0; $wedtol13 = 0;
      $precus1 = 0; $precus2 = 0; $precus3 = 0; $precus4 = 0; $precus5 = 0; $precus6 = 0; $precus7 = 0; $precus8 = 0; $precus9 = 0; $precus10 = 0; $precus11 = 0; $precus12 = 0; $precus13 = 0;
      $pretol1 = 0; $pretol2 = 0; $pretol3 = 0; $pretol4 = 0; $pretol5 = 0; $pretol6 = 0; $pretol7 = 0; $pretol8 = 0; $pretol9 = 0; $pretol10 = 0; $pretol11 = 0; $pretol12 = 0; $pretol13 = 0;
      $engcus1 = 0; $engcus2 = 0; $engcus3 = 0; $engcus4 = 0; $engcus5 = 0; $engcus6 = 0; $engcus7 = 0; $engcus8 = 0; $engcus9 = 0; $engcus10 = 0; $engcus11 = 0; $engcus12 = 0; $engcus13 = 0;
      $engtol1 = 0; $engtol2 = 0; $engtol3 = 0; $engtol4 = 0; $engtol5 = 0; $engtol6 = 0; $engtol7 = 0; $engtol8 = 0; $engtol9 = 0; $engtol10 = 0; $engtol11 = 0; $engtol12 = 0; $engtol13 = 0;
      $porcus1 = 0; $porcus2 = 0; $porcus3 = 0; $porcus4 = 0; $porcus5 = 0; $porcus6 = 0; $porcus7 = 0; $porcus8 = 0; $porcus9 = 0; $porcus10 = 0; $porcus11 = 0; $porcus12 = 0; $porcus13 = 0;
      $portol1 = 0; $portol2 = 0; $portol3 = 0; $portol4 = 0; $portol5 = 0; $portol6 = 0; $portol7 = 0; $portol8 = 0; $portol9 = 0; $portol10 = 0; $portol11 = 0; $portol12 = 0; $portol13 = 0;
      $othcus1 = 0; $othcus2 = 0; $othcus3 = 0; $othcus4 = 0; $othcus5 = 0; $othcus6 = 0; $othcus7 = 0; $othcus8 = 0; $othcus9 = 0; $othcus10 = 0; $othcus11 = 0; $othcus12 = 0; $othcus13 = 0;
      $othtol1 = 0; $othtol2 = 0; $othtol3 = 0; $othtol4 = 0; $othtol5 = 0; $othtol6 = 0; $othtol7 = 0; $othtol8 = 0; $othtol9 = 0; $othtol10 = 0; $othtol11 = 0; $othtol12 = 0; $othtol13 = 0;
      $tolcus1 = 0; $tolcus2 = 0; $tolcus3 = 0; $tolcus4 = 0; $tolcus5 = 0; $tolcus6 = 0; $tolcus7 = 0; $tolcus8 = 0; $tolcus9 = 0; $tolcus10 = 0; $tolcus11 = 0; $tolcus12 = 0; $tolcus13 = 0;
      $toltol1 = 0; $toltol2 = 0; $toltol3 = 0; $toltol4 = 0; $toltol5 = 0; $toltol6 = 0; $toltol7 = 0; $toltol8 = 0; $toltol9 = 0; $toltol10 = 0; $toltol11 = 0; $toltol12 = 0; $toltol13 = 0;
    
      for ($i = 0, $il = count($result->data); $i < $il; $i++) {
        $customer = $result->data[$i];
        if ((SimpleDate::toStamp(SimpleDate::create($yea[0], $mon[0], 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($yea[0], $mon[0], 31)))) {
          $tolcus1++;
          for ($j = 0, $jl = count($customer->orders); $j < $jl; $j++) {
            $order = $customer->orders[$j];
            if ($order->disposal->status) {
              continue;
            }
            if ($order->type == ORDER_TYPE_WEDDING) {
              $wedcus1++;
              $wedtol1 += (float)$order->price;
            }
            if ($order->type == ORDER_TYPE_PREWEDDING) {
              $pretol1 += (float)$order->price;
              $precus1++;
            }
            if ($order->type == ORDER_TYPE_PORTRAIT) {
              $portol1 += (float)$order->price;
              $porcus1++;
            }
            if ($order->type == ORDER_TYPE_ENGAGEMENT) {
              $engtol1 += (float)$order->price;
              $engcus1++;
            }
            if ($order->type == ORDER_TYPE_OTHER) {
              $othtol1 += (float)$order->price;
              $othcus1++;
            }
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[1], $mon[1], 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($yea[1], $mon[1], 31)))) {
          $tolcus2++;
          for ($j = 0, $jl = count($customer->orders); $j < $jl; $j++) {
            $order = $customer->orders[$j];
            if ($order->disposal->status) {
              continue;
            }
            if ($order->type == ORDER_TYPE_WEDDING) {
              $wedcus2++;
              $wedtol2 += $order->price;
            }
            if ($order->type == ORDER_TYPE_PREWEDDING) {
              $pretol2 += $order->price;
              $precus2++;
            }
            if ($order->type == ORDER_TYPE_PORTRAIT) {
              $portol2 += $order->price;
              $porcus2++;
            }
            if ($order->type == ORDER_TYPE_ENGAGEMENT) {
              $engtol2 += $order->price;
              $engcus2++;
            }
            if ($order->type == ORDER_TYPE_OTHER) {
              $othtol2 += $order->price;
              $othcus2++;
            }
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[2], $mon[2], 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($yea[2], $mon[2], 31)))) {
          $tolcus3++;
          for ($j = 0, $jl = count($customer->orders); $j < $jl; $j++) {
            $order = $customer->orders[$j];
            if ($order->disposal->status) {
              continue;
            }
            if ($order->type == ORDER_TYPE_WEDDING) {
              $wedcus3++;
              $wedtol3 += $order->price;
            }
            if ($order->type == ORDER_TYPE_PREWEDDING) {
              $pretol3 += $order->price;
              $precus3++;
            }
            if ($order->type == ORDER_TYPE_PORTRAIT) {
              $portol3 += $order->price;
              $porcus3++;
            }
            if ($order->type == ORDER_TYPE_ENGAGEMENT) {
              $engtol3 += $order->price;
              $engcus3++;
            }
            if ($order->type == ORDER_TYPE_OTHER) {
              $othtol3 += $order->price;
              $othcus3++;
            }
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[3], $mon[3], 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($yea[3], $mon[3], 31)))) {
          $tolcus4++;
          for ($j = 0, $jl = count($customer->orders); $j < $jl; $j++) {
            $order = $customer->orders[$j];
            if ($order->disposal->status) {
              continue;
            }
            if ($order->type == ORDER_TYPE_WEDDING) {
              $wedcus4++;
              $wedtol4 += $order->price;
            }
            if ($order->type == ORDER_TYPE_PREWEDDING) {
              $pretol4 += $order->price;
              $precus4++;
            }
            if ($order->type == ORDER_TYPE_PORTRAIT) {
              $portol4 += $order->price;
              $porcus4++;
            }
            if ($order->type == ORDER_TYPE_ENGAGEMENT) {
              $engtol4 += $order->price;
              $engcus4++;
            }
            if ($order->type == ORDER_TYPE_OTHER) {
              $othtol4 += $order->price;
              $othcus4++;
            }
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[4], $mon[4], 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($yea[4], $mon[4], 31)))) {
          $tolcus5++;
          for ($j = 0, $jl = count($customer->orders); $j < $jl; $j++) {
            $order = $customer->orders[$j];
            if ($order->disposal->status) {
              continue;
            }
            if ($order->type == ORDER_TYPE_WEDDING) {
              $wedcus5++;
              $wedtol5 += $order->price;
            }
            if ($order->type == ORDER_TYPE_PREWEDDING) {
              $pretol5 += $order->price;
              $precus5++;
            }
            if ($order->type == ORDER_TYPE_PORTRAIT) {
              $portol5 += $order->price;
              $porcus5++;
            }
            if ($order->type == ORDER_TYPE_ENGAGEMENT) {
              $engtol5 += $order->price;
              $engcus5++;
            }
            if ($order->type == ORDER_TYPE_OTHER) {
              $othtol5 += $order->price;
              $othcus5++;
            }
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[5], $mon[5], 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($yea[5], $mon[5], 31)))) {
          $tolcus6++;
          for ($j = 0, $jl = count($customer->orders); $j < $jl; $j++) {
            $order = $customer->orders[$j];
            if ($order->disposal->status) {
              continue;
            }
            if ($order->type == ORDER_TYPE_WEDDING) {
              $wedcus6++;
              $wedtol6 += $order->price;
            }
            if ($order->type == ORDER_TYPE_PREWEDDING) {
              $pretol6 += $order->price;
              $precus6++;
            }
            if ($order->type == ORDER_TYPE_PORTRAIT) {
              $portol6 += $order->price;
              $porcus6++;
            }
            if ($order->type == ORDER_TYPE_ENGAGEMENT) {
              $engtol6 += $order->price;
              $engcus6++;
            }
            if ($order->type == ORDER_TYPE_OTHER) {
              $othtol6 += $order->price;
              $othcus6++;
            }
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[6], $mon[6], 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($yea[6], $mon[6], 31)))) {
          $tolcus7++;
          for ($j = 0, $jl = count($customer->orders); $j < $jl; $j++) {
            $order = $customer->orders[$j];
            if ($order->disposal->status) {
              continue;
            }
            if ($order->type == ORDER_TYPE_WEDDING) {
              $wedcus7++;
              $wedtol7 += $order->price;
            }
            if ($order->type == ORDER_TYPE_PREWEDDING) {
              $pretol7 += $order->price;
              $precus7++;
            }
            if ($order->type == ORDER_TYPE_PORTRAIT) {
              $portol7 += $order->price;
              $porcus7++;
            }
            if ($order->type == ORDER_TYPE_ENGAGEMENT) {
              $engtol7 += $order->price;
              $engcus7++;
            }
            if ($order->type == ORDER_TYPE_OTHER) {
              $othtol7 += $order->price;
              $othcus7++;
            }
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[7], $mon[7], 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($yea[7], $mon[7], 31)))) {
          $tolcus8++;
          for ($j = 0, $jl = count($customer->orders); $j < $jl; $j++) {
            $order = $customer->orders[$j];
            if ($order->disposal->status) {
              continue;
            }
            if ($order->type == ORDER_TYPE_WEDDING) {
              $wedcus8++;
              $wedtol8 += $order->price;
            }
            if ($order->type == ORDER_TYPE_PREWEDDING) {
              $pretol8 += $order->price;
              $precus8++;
            }
            if ($order->type == ORDER_TYPE_PORTRAIT) {
              $portol8 += $order->price;
              $porcus8++;
            }
            if ($order->type == ORDER_TYPE_ENGAGEMENT) {
              $engtol8 += $order->price;
              $engcus8++;
            }
            if ($order->type == ORDER_TYPE_OTHER) {
              $othtol8 += $order->price;
              $othcus8++;
            }
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[8], $mon[8], 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($yea[8], $mon[8], 31)))) {
          $tolcus9++;
          for ($j = 0, $jl = count($customer->orders); $j < $jl; $j++) {
            $order = $customer->orders[$j];
            if ($order->disposal->status) {
              continue;
            }
            if ($order->type == ORDER_TYPE_WEDDING) {
              $wedcus9++;
              $wedtol9 += $order->price;
            }
            if ($order->type == ORDER_TYPE_PREWEDDING) {
              $pretol9 += $order->price;
              $precus9++;
            }
            if ($order->type == ORDER_TYPE_PORTRAIT) {
              $portol9 += $order->price;
              $porcus9++;
            }
            if ($order->type == ORDER_TYPE_ENGAGEMENT) {
              $engtol9 += $order->price;
              $engcus9++;
            }
            if ($order->type == ORDER_TYPE_OTHER) {
              $othtol9 += $order->price;
              $othcus9++;
            }
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[9], $mon[9], 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($yea[9], $mon[9], 31)))) {
          $tolcus10++;
          for ($j = 0, $jl = count($customer->orders); $j < $jl; $j++) {
            $order = $customer->orders[$j];
            if ($order->disposal->status) {
              continue;
            }
            if ($order->type == ORDER_TYPE_WEDDING) {
              $wedcus10++;
              $wedtol10 += $order->price;
            }
            if ($order->type == ORDER_TYPE_PREWEDDING) {
              $pretol10 += $order->price;
              $precus10++;
            }
            if ($order->type == ORDER_TYPE_PORTRAIT) {
              $portol10 += $order->price;
              $porcus10++;
            }
            if ($order->type == ORDER_TYPE_ENGAGEMENT) {
              $engtol10 += $order->price;
              $engcus10++;
            }
            if ($order->type == ORDER_TYPE_OTHER) {
              $othtol10 += $order->price;
              $othcus10++;
            }
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[10], $mon[10], 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($yea[10], $mon[10], 31)))) {
          $tolcus11++;
          for ($j = 0, $jl = count($customer->orders); $j < $jl; $j++) {
            $order = $customer->orders[$j];
            if ($order->disposal->status) {
              continue;
            }
            if ($order->type == ORDER_TYPE_WEDDING) {
              $wedcus11++;
              $wedtol11 += $order->price;
            }
            if ($order->type == ORDER_TYPE_PREWEDDING) {
              $pretol11 += $order->price;
              $precus11++;
            }
            if ($order->type == ORDER_TYPE_PORTRAIT) {
              $portol11 += $order->price;
              $porcus11++;
            }
            if ($order->type == ORDER_TYPE_ENGAGEMENT) {
              $engtol11 += $order->price;
              $engcus11++;
            }
            if ($order->type == ORDER_TYPE_OTHER) {
              $othtol11 += $order->price;
              $othcus11++;
            }
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[11], $mon[11], 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($yea[11], $mon[11], 31)))) {
          $tolcus12++;
          for ($j = 0, $jl = count($customer->orders); $j < $jl; $j++) {
            $order = $customer->orders[$j];
            if ($order->disposal->status) {
              continue;
            }
            if ($order->type == ORDER_TYPE_WEDDING) {
              $wedcus11++;
              $wedtol12 += $order->price;
            }
            if ($order->type == ORDER_TYPE_PREWEDDING) {
              $pretol12 += $order->price;
              $precus12++;
            }
            if ($order->type == ORDER_TYPE_PORTRAIT) {
              $portol12 += $order->price;
              $porcus12++;
            }
            if ($order->type == ORDER_TYPE_ENGAGEMENT) {
              $engtol12 += $order->price;
              $engcus12++;
            }
            if ($order->type == ORDER_TYPE_OTHER) {
              $othtol12 += $order->price;
              $othcus12++;
            }
          }
        }
        if ((SimpleDate::toStamp(SimpleDate::create($yea[12], $mon[12], 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($yea[12], $mon[12], 31)))) {
          $tolcus13++;
          for ($j = 0, $jl = count($customer->orders); $j < $jl; $j++) {
            $order = $customer->orders[$j];
            if ($order->disposal->status) {
              continue;
            } 
            if ($order->type == ORDER_TYPE_WEDDING) {
              $wedcus13++;
              $wedtol13 += $order->price;
            }
            if ($order->type == ORDER_TYPE_PREWEDDING) {
              $pretol13 += $order->price;
              $precus13++;
            }
            if ($order->type == ORDER_TYPE_PORTRAIT) {
              $portol13 += $order->price;
              $porcus13++;
            }
            if ($order->type == ORDER_TYPE_ENGAGEMENT) {
              $engtol13 += $order->price;
              $engcus13++;
            }
            if ($order->type == ORDER_TYPE_OTHER) {
              $othtol13 += $order->price;
              $othcus13++;
            }
          }
        }
      }
      $toltol1 = $wedtol1 + $pretol1 + $portol1 + $engtol1 + $othtol1;
      $toltol2 = $wedtol2 + $pretol2 + $portol2 + $engtol2 + $othtol2;
      $toltol3 = $wedtol3 + $pretol3 + $portol3 + $engtol3 + $othtol3;
      $toltol4 = $wedtol4 + $pretol4 + $portol4 + $engtol4 + $othtol4;
      $toltol5 = $wedtol5 + $pretol5 + $portol5 + $engtol5 + $othtol5;
      $toltol6 = $wedtol6 + $pretol6 + $portol6 + $engtol6 + $othtol6;
      $toltol7 = $wedtol7 + $pretol7 + $portol7 + $engtol7 + $othtol7;
      $toltol8 = $wedtol8 + $pretol8 + $portol8 + $engtol8 + $othtol8;
      $toltol9 = $wedtol9 + $pretol9 + $portol9 + $engtol9 + $othtol9;
      $toltol10 = $wedtol10 + $pretol10 + $portol10 + $engtol10 + $othtol10;
      $toltol11 = $wedtol11 + $pretol11 + $portol11 + $engtol11 + $othtol11;
      $toltol12 = $wedtol12 + $pretol12 + $portol12 + $engtol12 + $othtol12;
      $toltol13 = $wedtol13 + $pretol13 + $portol13 + $engtol13 + $othtol13;
      $result = array();
      $res1 = new stdClass(); $res2 = new stdClass(); $res3 = new stdClass(); $res4 = new stdClass(); $res5 = new stdClass();
      $res6 = new stdClass(); $res7 = new stdClass(); $res8 = new stdClass(); $res9 = new stdClass(); $res10 = new stdClass();
      $res11 = new stdClass(); $res12 = new stdClass(); $res13 = new stdClass();
      
      $res1->month = $mon[0];
      $res1->year = $yea[0];
      $res1->wedcus = $wedcus1;
      $res1->wedtol = $wedtol1;
      $res1->precus = $precus1;
      $res1->pretol = $pretol1;
      $res1->porcus = $porcus1;
      $res1->portol = $portol1;
      $res1->engcus = $engcus1;
      $res1->engtol = $engtol1;
      $res1->othcus = $othcus1;
      $res1->othtol = $othtol1;
      $res1->tolcus = $tolcus1;
      $res1->toltol = $toltol1;
      
      $res2->month = $mon[1];
      $res2->year = $yea[1];
      $res2->wedcus = $wedcus2;
      $res2->wedtol = $wedtol2;
      $res2->precus = $precus2;
      $res2->pretol = $pretol2;
      $res2->porcus = $porcus2;
      $res2->portol = $portol2;
      $res2->engcus = $engcus2;
      $res2->engtol = $engtol2;
      $res2->othcus = $othcus2;
      $res2->othtol = $othtol2;
      $res2->tolcus = $tolcus2;
      $res2->toltol = $toltol2;
      
      $res3->month = $mon[2];
      $res3->year = $yea[2];
      $res3->wedcus = $wedcus3;
      $res3->wedtol = $wedtol3;
      $res3->precus = $precus3;
      $res3->pretol = $pretol3;
      $res3->porcus = $porcus3;
      $res3->portol = $portol3;
      $res3->engcus = $engcus3;
      $res3->engtol = $engtol3;
      $res3->othcus = $othcus3;
      $res3->othtol = $othtol3;
      $res3->tolcus = $tolcus3;
      $res3->toltol = $toltol3;
      
      $res4->month = $mon[3];
      $res4->year = $yea[3];
      $res4->wedcus = $wedcus4;
      $res4->wedtol = $wedtol4;
      $res4->precus = $precus4;
      $res4->pretol = $pretol4;
      $res4->porcus = $porcus4;
      $res4->portol = $portol4;
      $res4->engcus = $engcus4;
      $res4->engtol = $engtol4;
      $res4->othcus = $othcus4;
      $res4->othtol = $othtol4;
      $res4->tolcus = $tolcus4;
      $res4->toltol = $toltol4;
      
      $res5->month = $mon[4];
      $res5->year = $yea[4];
      $res5->wedcus = $wedcus5;
      $res5->wedtol = $wedtol5;
      $res5->precus = $precus5;
      $res5->pretol = $pretol5;
      $res5->porcus = $porcus5;
      $res5->portol = $portol5;
      $res5->engcus = $engcus5;
      $res5->engtol = $engtol5;
      $res5->othcus = $othcus5;
      $res5->othtol = $othtol5;
      $res5->tolcus = $tolcus5;
      $res5->toltol = $toltol5;
      
      $res6->month = $mon[5];
      $res6->year = $yea[5];
      $res6->wedcus = $wedcus6;
      $res6->wedtol = $wedtol6;
      $res6->precus = $precus6;
      $res6->pretol = $pretol6;
      $res6->porcus = $porcus6;
      $res6->portol = $portol6;
      $res6->engcus = $engcus6;
      $res6->engtol = $engtol6;
      $res6->othcus = $othcus6;
      $res6->othtol = $othtol6;
      $res6->tolcus = $tolcus6;
      $res6->toltol = $toltol6;
      
      $res7->month = $mon[6];
      $res7->year = $yea[6];
      $res7->wedcus = $wedcus7;
      $res7->wedtol = $wedtol7;
      $res7->precus = $precus7;
      $res7->pretol = $pretol7;
      $res7->porcus = $porcus7;
      $res7->portol = $portol7;
      $res7->engcus = $engcus7;
      $res7->engtol = $engtol7;
      $res7->othcus = $othcus7;
      $res7->othtol = $othtol7;
      $res7->tolcus = $tolcus7;
      $res7->toltol = $toltol7;
      
      $res8->month = $mon[7];
      $res8->year = $yea[7];
      $res8->wedcus = $wedcus8;
      $res8->wedtol = $wedtol8;
      $res8->precus = $precus8;
      $res8->pretol = $pretol8;
      $res8->porcus = $porcus8;
      $res8->portol = $portol8;
      $res8->engcus = $engcus8;
      $res8->engtol = $engtol8;
      $res8->othcus = $othcus8;
      $res8->othtol = $othtol8;
      $res8->tolcus = $tolcus8;
      $res8->toltol = $toltol8;
      
      $res9->month = $mon[8];
      $res9->year = $yea[8];
      $res9->wedcus = $wedcus9;
      $res9->wedtol = $wedtol9;
      $res9->precus = $precus9;
      $res9->pretol = $pretol9;
      $res9->porcus = $porcus9;
      $res9->portol = $portol9;
      $res9->engcus = $engcus9;
      $res9->engtol = $engtol9;
      $res9->othcus = $othcus9;
      $res9->othtol = $othtol9;
      $res9->tolcus = $tolcus9;
      $res9->toltol = $toltol9;
      
      $res10->month = $mon[9];
      $res10->year = $yea[9];
      $res10->wedcus = $wedcus10;
      $res10->wedtol = $wedtol10;
      $res10->precus = $precus10;
      $res10->pretol = $pretol10;
      $res10->porcus = $porcus10;
      $res10->portol = $portol10;
      $res10->engcus = $engcus10;
      $res10->engtol = $engtol10;
      $res10->othcus = $othcus10;
      $res10->othtol = $othtol10;
      $res10->tolcus = $tolcus10;
      $res10->toltol = $toltol10;
      
      $res11->month = $mon[10];
      $res11->year = $yea[10];
      $res11->wedcus = $wedcus11;
      $res11->wedtol = $wedtol11;
      $res11->precus = $precus11;
      $res11->pretol = $pretol11;
      $res11->porcus = $porcus11;
      $res11->portol = $portol11;
      $res11->engcus = $engcus11;
      $res11->engtol = $engtol11;
      $res11->othcus = $othcus11;
      $res11->othtol = $othtol11;
      $res11->tolcus = $tolcus11;
      $res11->toltol = $toltol11;
      
      $res12->month = $mon[11];
      $res12->year = $yea[11];
      $res12->wedcus = $wedcus12;
      $res12->wedtol = $wedtol12;
      $res12->precus = $precus12;
      $res12->pretol = $pretol12;
      $res12->porcus = $porcus12;
      $res12->portol = $portol12;
      $res12->engcus = $engcus12;
      $res12->engtol = $engtol12;
      $res12->othcus = $othcus12;
      $res12->othtol = $othtol12;
      $res12->tolcus = $tolcus12;
      $res12->toltol = $toltol12;
      
      $res13->month = $mon[12];
      $res13->year = $yea[12];
      $res13->wedcus = $wedcus13;
      $res13->wedtol = $wedtol13;
      $res13->precus = $precus13;
      $res13->pretol = $pretol13;
      $res13->porcus = $porcus13;
      $res13->portol = $portol13;
      $res13->engcus = $engcus13;
      $res13->engtol = $engtol13;
      $res13->othcus = $othcus13;
      $res13->othtol = $othtol13;
      $res13->tolcus = $tolcus13;
      $res13->toltol = $toltol13;
      array_push($result, $res1, $res2, $res3, $res4, $res5, $res6, $res7, $res8, $res9, $res10, $res11, $res12, $res13);
      echo json_encode($result);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function iesumexpense($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', '1 = 1');
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $condition .= ($createdFrom) ? ' AND (d.payDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.payDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $orm = classToOrm('expense');
  if ($orm) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      $exp = 0; $cash = 0; $noncash = 0; $rest = array(); $exist;
      for ($i = 0, $il = count($result->data); $i < $il; $i++) {
        $expense = $result->data[$i];
        if ($expense->status == -1) {
          continue;
        }
        $exp += $expense->amount;
        if ($expense->method == 'C') {
          $cash += $expense->amount;
        } else {
          $noncash += $expense->amount;
        }
        $exist = false;
        for ($j = 0, $jl = count($rest); $j < $jl; $j++) {
          $tmp = $rest[$j];
          if ($tmp->name == $expense->particular) {
            $tmp->value += $expense->amount;
            $exist = true;
          }
        }
        if (!$exist) {
          $m = new stdClass();
          $m->name = $expense->particular;
          $m->value = $expense->amount;
          array_push($rest, $m);
        }
      }
      $top = new stdClass();
      $top->expense = $exp;
      $top->cash = $cash;
      $top->noncash = $noncash;

      $months = _months(json_decode($createdFrom), json_decode($createdTo));

      $bottom = array();
      foreach ($months as &$tmp) {
        $start = SimpleDate::toStamp(SimpleDate::create($tmp->year, $tmp->month, 1));
        $end = SimpleDate::toStamp(SimpleDate::create($tmp->year, $tmp->month, _days($tmp->year, $tmp->month)));
        $b = new stdClass();
        $b->month = $tmp->month;
        $b->year = $tmp->year;
        $b->ecash = 0;
        $b->enoncash = 0;
        for ($j = 0, $jl = count($result->data); $j < $jl; $j++) {
          $expense = $result->data[$j];
          if ($expense->status == -1) {
            continue;
          }
          if (($start <= SimpleDate::toStamp($expense->createdDate)) && (SimpleDate::toStamp($expense->createdDate) <= $end)) {
            if ($expense->method == 'C') {
              $b->ecash += $expense->amount;
            } else {
              $b->enoncash += $expense->amount;
            }
          }
        }
        array_push($bottom, $b);
      }
      $res = new stdClass();
      $res->top = $top;
      $res->middle = $rest;
      $res->bottom = $bottom;
      echo json_encode($res);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function iestatspackage($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);

  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $condition .= ($createdFrom) ? ' AND (d.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $orm = classToOrm('customer');
  $orm1 = classToOrm('package');
  if ($orm && $orm1) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      $result1 = $orm1->find($myManager, NULL, NULL, NULL, NULL, NULL, NULL);
      
      $wedPPackage = array(); $wedVPackage = array(); 
      $prePPackage = array(); $preVPackage = array();  
      $porPPackage = array(); $porVPackage = array(); 
      $engPPackage = array(); $engVPackage = array(); 
      $othPPackage = array(); $othVPackage = array();
      for ($i = 0, $il = count($result1->data); $i < $il; $i++) {
        $package = $result1->data[$i];
        if ($package->orderInc == ORDER_TYPE_WEDDING) {
          if ($package->type == PACKAGE_TYPE_PHOTO) {
            array_push($wedPPackage, $package->name);
          } else {
            array_push($wedVPackage, $package->name);
          }
        } else if ($package->orderInc == ORDER_TYPE_PREWEDDING) {
          if ($package->type == PACKAGE_TYPE_PHOTO) {
            array_push($prePPackage, $package->name);
          } else {
            array_push($preVPackage, $package->name);
          }
        } else if ($package->orderInc == ORDER_TYPE_PORTRAIT) {
          if ($package->type == PACKAGE_TYPE_PHOTO) {
            array_push($porPPackage, $package->name);
          } else {
            array_push($porVPackage, $package->name);
          }
        } else if ($package->orderInc == ORDER_TYPE_ENGAGEMENT) {
          if ($package->type == PACKAGE_TYPE_PHOTO) {
            array_push($engPPackage, $package->name);
          } else {
            array_push($engVPackage, $package->name);
          }
        } else {
          if ($package->type == PACKAGE_TYPE_PHOTO) {
            array_push($othPPackage, $package->name);
          } else {
            array_push($othVPackage, $package->name);
          }
        }
      }
      $wedPhoto = array(); $wedVideo = array();
      $prePhoto = array(); $preVideo = array();
      $porPhoto = array(); $porVideo = array();
      $engPhoto = array(); $engVideo = array();
      $othPhoto = array(); $othVideo = array();
      for ($i = 0, $il = count($result->data); $i < $il; $i++) {
        $customer = $result->data[$i];
        for ($j = 0, $jl = count($customer->orders); $j < $jl; $j++) {
          $order = $customer->orders[$j];
          if ($order->type == ORDER_TYPE_WEDDING) {
            $photo = $wedPPackage;
            $video = $wedVPackage;
            
            if ($order->photo) {
              for ($k = 0, $kl = count($photo); $k < $kl; $k++) {
                $p = $photo[$k];
                $wc = 1; $exist = false;
                if ($order->photo->name == $p) {
                  for($m = 0, $ml = count($wedPhoto); $m < $ml; $m++) {
                    $wp = $wedPhoto[$m];
                    if ($wp->name == $p) {
                      $wp->value = ++$wc;
                      $exist = true;
                    }
                  }
                  if (!$exist) {
                    $obj = new stdClass();
                    $obj->name = $p;
                    $obj->value = $wc;
                    array_push($wedPhoto, $obj);
                  }
                }
              }
            }
            if ($order->video) {
              for ($k = 0, $kl = count($video); $k < $kl; $k++) {
                $p = $video[$k];
                $wc = 1; $exist = false;
                if ($order->video->name == $p) {
                  for($m = 0, $ml = count($wedVideo); $m < $ml; $m++) {
                    $wp = $wedVideo[$m];
                    if ($wp->name == $p) {
                      $wp->value = ++$wc;
                      $exist = true;
                    }
                  }
                  if (!$exist) {
                    $obj = new stdClass();
                    $obj->name = $p;
                    $obj->value = $wc;
                    array_push($wedVideo, $obj);
                  }
                }
              }
            }
          } else if ($order->type == ORDER_TYPE_PREWEDDING) {
            $photo = $prePPackage;
            $video = $preVPackage;
            
            if ($order->photo) {
              for ($k = 0, $kl = count($photo); $k < $kl; $k++) {
                $p = $photo[$k];
                $wc = 1; $exist = false;
                if ($order->photo->name == $p) {
                  for($m = 0, $ml = count($prePhoto); $m < $ml; $m++) {
                    $wp = $prePhoto[$m];
                    if ($wp->name == $p) {
                      $wp->value = ++$wc;
                      $exist = true;
                    }
                  }
                  if (!$exist) {
                    $obj = new stdClass();
                    $obj->name = $p;
                    $obj->value = $wc;
                    array_push($prePhoto, $obj);
                  }
                }
              }
            }
            if ($order->video) {
              for ($k = 0, $kl = count($video); $k < $kl; $k++) {
                $p = $video[$k];
                $wc = 1; $exist = false;
                if ($order->video->name == $p) {
                  for($m = 0, $ml = count($preVideo); $m < $ml; $m++) {
                    $wp = $preVideo[$m];
                    if ($wp->name == $p) {
                      $wp->value = ++$wc;
                      $exist = true;
                    }
                  }
                  if (!$exist) {
                    $obj = new stdClass();
                    $obj->name = $p;
                    $obj->value = $wc;
                    array_push($preVideo, $obj);
                  }
                }
              }
            }
          } else if ($order->type == ORDER_TYPE_PORTRAIT) {
            $photo = $porPPackage;
            $video = $porVPackage;
            
            if ($order->photo) {
              for ($k = 0, $kl = count($photo); $k < $kl; $k++) {
                $p = $photo[$k];
                $wc = 1; $exist = false;
                if ($order->photo->name == $p) {
                  for($m = 0, $ml = count($porPhoto); $m < $ml; $m++) {
                    $wp = $porPhoto[$m];
                    if ($wp->name == $p) {
                      $wp->value = ++$wc;
                      $exist = true;
                    }
                  }
                  if (!$exist) {
                    $obj = new stdClass();
                    $obj->name = $p;
                    $obj->value = $wc;
                    array_push($porPhoto, $obj);
                  }
                }
              }
            }
            if ($order->video) {
              for ($k = 0, $kl = count($video); $k < $kl; $k++) {
                $p = $video[$k];
                $wc = 1; $exist = false;
                if ($order->video->name == $p) {
                  for($m = 0, $ml = count($porVideo); $m < $ml; $m++) {
                    $wp = $porVideo[$m];
                    if ($wp->name == $p) {
                      $wp->value = ++$wc;
                      $exist = true;
                    }
                  }
                  if (!$exist) {
                    $obj = new stdClass();
                    $obj->name = $p;
                    $obj->value = $wc;
                    array_push($porVideo, $obj);
                  }
                }
              }
            }
          } else if ($order->type == ORDER_TYPE_ENGAGEMENT) {
            $photo = $engPPackage;
            $video = $engVPackage;
            
            if ($order->photo) {
              for ($k = 0, $kl = count($photo); $k < $kl; $k++) {
                $p = $photo[$k];
                $wc = 1; $exist = false;
                if ($order->photo->name == $p) {
                  for($m = 0, $ml = count($engPhoto); $m < $ml; $m++) {
                    $wp = $engPhoto[$m];
                    if ($wp->name == $p) {
                      $wp->value = ++$wc;
                      $exist = true;
                    }
                  }
                  if (!$exist) {
                    $obj = new stdClass();
                    $obj->name = $p;
                    $obj->value = $wc;
                    array_push($engPhoto, $obj);
                  }
                }
              }
            }
            if ($order->video) {
              for ($k = 0, $kl = count($video); $k < $kl; $k++) {
                $p = $video[$k];
                $wc = 1; $exist = false;
                if ($order->video->name == $p) {
                  for($m = 0, $ml = count($engVideo); $m < $ml; $m++) {
                    $wp = $engVideo[$m];
                    if ($wp->name == $p) {
                      $wp->value = ++$wc;
                      $exist = true;
                    }
                  }
                  if (!$exist) {
                    $obj = new stdClass();
                    $obj->name = $p;
                    $obj->value = $wc;
                    array_push($engVideo, $obj);
                  }
                }
              }
            }
          } else {
            $photo = $othPPackage;
            $video = $othVPackage;
            
            if ($order->photo) {
              for ($k = 0, $kl = count($photo); $k < $kl; $k++) {
                $p = $photo[$k];
                $wc = 1; $exist = false;
                if ($order->photo->name == $p) {
                  for($m = 0, $ml = count($othPhoto); $m < $ml; $m++) {
                    $wp = $othPhoto[$m];
                    if ($wp->name == $p) {
                      $wp->value = ++$wc;
                      $exist = true;
                    }
                  }
                  if (!$exist) {
                    $obj = new stdClass();
                    $obj->name = $p;
                    $obj->value = $wc;
                    array_push($othPhoto, $obj);
                  }
                }
              }
            }
            if ($order->video) {
              for ($k = 0, $kl = count($video); $k < $kl; $k++) {
                $p = $video[$k];
                $wc = 1; $exist = false;
                if ($order->video->name == $p) {
                  for($m = 0, $ml = count($othVideo); $m < $ml; $m++) {
                    $wp = $othVideo[$m];
                    if ($wp->name == $p) {
                      $wp->value = ++$wc;
                      $exist = true;
                    }
                  }
                  if (!$exist) {
                    $obj = new stdClass();
                    $obj->name = $p;
                    $obj->value = $wc;
                    array_push($othVideo, $obj);
                  }
                }
              }
            }
          } 
        }
      }
      $total = new stdClass();
      $total->wedp = $wedPhoto;
      $total->wedv = $wedVideo;
      $total->prep = $prePhoto;
      $total->prev = $preVideo;
      $total->porp = $porPhoto;
      $total->porv = $porVideo;
      $total->engp = $engPhoto;
      $total->engv = $engVideo;
      $total->othp = $othPhoto;
      $total->othv = $othVideo;
      echo json_encode($total);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function iestatsproduct($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);

  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $condition .= ($createdFrom) ? ' AND (d.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $orm = classToOrm('customer');
  $orm1 = classToOrm('sample');
  if ($orm && $orm1) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      $result1 = $orm1->find($myManager, NULL, NULL, 'd.name', NULL, NULL, NULL);
      
      $sample = array();
      $product = array();
      for ($k = 0, $kl = count($result1->data); $k < $kl; $k++) {
        $p = $result1->data[$k];
        array_push($sample, $p->name);
      }
      for ($i = 0, $il = count($result->data); $i < $il; $i++) {
        $customer = $result->data[$i];
        for ($j = 0, $jl = count($customer->orders); $j < $jl; $j++) {
          $order = $customer->orders[$j];
          for ($u = 0, $ul = count($order->purchases); $u < $ul; $u++) {
            $purchase = $order->purchases[$u];
            for($m = 0, $ml = count($sample); $m < $ml; $m++) {
              $wp = $sample[$m];
              $exist = false; $pc = 0;
              if ($wp == $purchase->product->name) {
                for ($k = 0, $kl = count($product); $k < $kl; $k++) {
                  $p = $product[$k];
                  if ($p->name == $wp) {
                    $p->value += $purchase->quantity;
                    $exist = true;
                  }
                }
                if (!$exist) {
                  $obj = new stdClass();
                  $obj->name = $wp;
                  $obj->value = $purchase->quantity;
                  array_push($product, $obj);
                }
              } else {
                for ($k = 0, $kl = count($product); $k < $kl; $k++) {
                  $p = $product[$k];
                  if ($p->name == $wp) {
                    $exist = true;
                  }
                }
                if (!$exist) {
                  $obj = new stdClass();
                  $obj->name = $wp;
                  $obj->value = $pc;
                  array_push($product, $obj);
                }
              }
            }
          }
        }
      }
      echo json_encode($product);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function iestatssource($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);

  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $condition .= ($createdFrom) ? ' AND (d.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $orm = classToOrm('customer');
  $orm1 = classToOrm('isource');
  if ($orm && $orm1) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      $result1 = $orm1->find($myManager, NULL, NULL, 'd.name', NULL, NULL, NULL);
      
      $source = array();
      $sourceResult = array();
      for ($k = 0, $kl = count($result1->data); $k < $kl; $k++) {
        $p = $result1->data[$k];
        array_push($source, $p->name);
      }
      for ($i = 0, $il = count($result->data); $i < $il; $i++) {
        $customer = $result->data[$i];
        for($m = 0, $ml = count($source); $m < $ml; $m++) {
          $sc = $source[$m];
          $rev = 0; $exist = false; $c = 1;
          if ($customer->source == $sc) {
            for ($k = 0, $kl = count($sourceResult); $k < $kl; $k++) {
              $s = $sourceResult[$k];
              if ($s->name == $sc) {
                $s->value++;
                $exist = true;
              }
            }
            for ($j = 0, $jl = count($customer->orders); $j < $jl; $j++) {
              $order = $customer->orders[$j];
              for ($k = 0, $kl = count($sourceResult); $k < $kl; $k++) {
                $s = $sourceResult[$k];
                if ($s->name == $sc) {
                  $s->revenue += $order->price;
                  $exist = true;
                }
              }
              if (!$exist) {
                $obj = new stdClass();
                $obj->name = $sc;
                $obj->value = $c;
                $obj->revenue = $order->price;
                array_push($sourceResult, $obj);
              }
            }
          }
        }
      }
      echo json_encode($sourceResult);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function iestatsculture($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);

  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $condition .= ($createdFrom) ? ' AND (d.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $orm = classToOrm('customer');
  $orm1 = classToOrm('iculture');
  if ($orm && $orm1) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      $result1 = $orm1->find($myManager, NULL, NULL, 'd.name', NULL, NULL, NULL);
      
      $culture = array();
      $cultureResult = array();
      for ($k = 0, $kl = count($result1->data); $k < $kl; $k++) {
        $cul = $result1->data[$k];
        array_push($culture, $cul->name);
      }
      for ($i = 0, $il = count($result->data); $i < $il; $i++) {
        $customer = $result->data[$i];
        for($m = 0, $ml = count($culture); $m < $ml; $m++) {
          $cult = $culture[$m];
          $exist = false; $c = 1;
          if ($customer->culture == $cult) {
            for ($k = 0, $kl = count($cultureResult); $k < $kl; $k++) {
              $res = $cultureResult[$k];
              if ($res->name == $cult) {
                $res->value++;
                $exist = true;
              }
            }
            for ($j = 0, $jl = count($customer->orders); $j < $jl; $j++) {
              $order = $customer->orders[$j];
              for ($k = 0, $kl = count($cultureResult); $k < $kl; $k++) {
                $res = $cultureResult[$k];
                if ($res->name == $cult) {
                  $res->revenue += $order->price;
                  $exist = true;
                }
              }
              if (!$exist) {
                $obj = new stdClass();
                $obj->name = $cult;
                $obj->value = $c;
                $obj->revenue = $order->price;
                array_push($cultureResult, $obj);
              }
            }
          }
        }
      }
      echo json_encode($cultureResult);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function iestatsreception($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);

  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $condition .= ($createdFrom) ? ' AND (d.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $orm = classToOrm('customer');
  $orm1 = classToOrm('ireception');
  if ($orm && $orm1) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      $result1 = $orm1->find($myManager, NULL, NULL, 'd.name', NULL, NULL, NULL);
      
      $reception = array();
      $receptionResult = array();
      for ($k = 0, $kl = count($result1->data); $k < $kl; $k++) {
        $cul = $result1->data[$k];
        array_push($reception, $cul->name);
      }
      for ($i = 0, $il = count($result->data); $i < $il; $i++) {
        $customer = $result->data[$i];
        for($m = 0, $ml = count($reception); $m < $ml; $m++) {
          $rec = $reception[$m];
          $exist = false; $c = 1;
          if ($customer->reception == $rec) {
            for ($k = 0, $kl = count($receptionResult); $k < $kl; $k++) {
              $res = $receptionResult[$k];
              if ($res->name == $rec) {
                $res->value++;
                $exist = true;
              }
            }
            if (!$exist) {
              $obj = new stdClass();
              $obj->name = $rec;
              $obj->value = $c;
              array_push($receptionResult, $obj);
            }
          }
        }
      }
      echo json_encode($receptionResult);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function iestatsreceptioncustomers($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);

  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $condition .= ($createdFrom) ? ' AND (d.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $orm = classToOrm('customer');
  if ($orm) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      $res = array();
      for ($i = 0, $il = count($result->data); $i < $il; $i++) {
        $customer = $result->data[$i];
        array_push($res, $customer->id);
      }
      echo json_encode($res);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function iestatsceremony($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);

  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $condition .= ($createdFrom) ? ' AND (d.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $orm = classToOrm('customer');
  $orm1 = classToOrm('iceremony');
  if ($orm && $orm1) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      $result1 = $orm1->find($myManager, NULL, NULL, 'd.name', NULL, NULL, NULL);
      
      $ceremony = array();
      $ceremonyResult = array();
      for ($k = 0, $kl = count($result1->data); $k < $kl; $k++) {
        $ce = $result1->data[$k];
        array_push($ceremony, $ce->name);
      }
      for ($i = 0, $il = count($result->data); $i < $il; $i++) {
        $customer = $result->data[$i];
        for($m = 0, $ml = count($ceremony); $m < $ml; $m++) {
          $cer = $ceremony[$m];
          $exist = false; $c = 1;
          if ($customer->ceremony == $cer) {
            for ($k = 0, $kl = count($ceremonyResult); $k < $kl; $k++) {
              $res = $ceremonyResult[$k];
              if ($res->name == $cer) {
                $res->value++;
                $exist = true;
              }
            }
            if (!$exist) {
              $obj = new stdClass();
              $obj->name = $cer;
              $obj->value = $c;
              array_push($ceremonyResult, $obj);
            }
          }
        }
      }
      echo json_encode($ceremonyResult);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function iestatsceremonycustomers($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);

  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $condition .= ($createdFrom) ? ' AND (d.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $orm = classToOrm('customer');
  if ($orm) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      $res = array();
      for ($i = 0, $il = count($result->data); $i < $il; $i++) {
        $customer = $result->data[$i];
        array_push($res, $customer->id);
      }
      echo json_encode($res);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function iecompbymonth($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', 'd.createdDate');
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);

  $condition .= ($createdFrom) ? ' AND (d.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $orm = classToOrm('customer');
  if ($orm) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      $year = array(); $month = array();
      for ($i = 0, $il = count($result->data); $i < $il; $i++) {
        $customer = $result->data[$i];
        $exist = false;
        for ($j = 0, $jl = count($year); $j < $jl; $j++) {
          $y = $year[$j];
          if ($y == $customer->createdDate->year) {
            $exist = true;
          }
        }
        if (!$exist) {
          array_push($year, $customer->createdDate->year);
        }
      }
      for ($i = 0, $il = count($result->data); $i < $il; $i++) {
        $customer = $result->data[$i];
        $exist = false;
        for ($j = 0, $jl = count($month); $j < $jl; $j++) {
          $m = $month[$j];
          if ($m == $customer->createdDate->month) {
            $exist = true;
          }
        }
        if (!$exist) {
          array_push($month, $customer->createdDate->month);
        }
      }
      $res = array(); $final = array();
      $a1 = array();$a2 = array();$a3 = array();$a4 = array();$a5 = array();$a6 = array();$a7 = array();$a8 = array();$a9 = array();
      $a10 = array();$a11 = array();$a12 = array();
      for ($k = 0, $kl = count($year); $k < $kl; $k++) {
        $ye = $year[$k];
        $wed1 = 0; $wedcus1 = 0; $pre1 = 0; $precus1 = 0; $por1 = 0; $porcus1 = 0; $eng1 = 0; $engcus1 = 0; $oth1 = 0; $othcus1 = 0; $tolcus1 = 0;
        $wed2 = 0; $wedcus2 = 0; $pre2 = 0; $precus2 = 0; $por2 = 0; $porcus2 = 0; $eng2 = 0; $engcus2 = 0; $oth2 = 0; $othcus2 = 0; $tolcus2 = 0;
        $wed3 = 0; $wedcus3 = 0; $pre3 = 0; $precus3 = 0; $por3 = 0; $porcus3 = 0; $eng3 = 0; $engcus3 = 0; $oth3 = 0; $othcus3 = 0; $tolcus3 = 0;
        $wed4 = 0; $wedcus4 = 0; $pre4 = 0; $precus4 = 0; $por4 = 0; $porcus4 = 0; $eng4 = 0; $engcus4 = 0; $oth4 = 0; $othcus4 = 0; $tolcus4 = 0;
        $wed5 = 0; $wedcus5 = 0; $pre5 = 0; $precus5 = 0; $por5 = 0; $porcus5 = 0; $eng5 = 0; $engcus5 = 0; $oth5 = 0; $othcus5 = 0; $tolcus5 = 0;
        $wed6 = 0; $wedcus6 = 0; $pre6 = 0; $precus6 = 0; $por6 = 0; $porcus6 = 0; $eng6 = 0; $engcus6 = 0; $oth6 = 0; $othcus6 = 0; $tolcus6 = 0;
        $wed7 = 0; $wedcus7 = 0; $pre7 = 0; $precus7 = 0; $por7 = 0; $porcus7 = 0; $eng7 = 0; $engcus7 = 0; $oth7 = 0; $othcus7 = 0; $tolcus7 = 0;
        $wed8 = 0; $wedcus8 = 0; $pre8 = 0; $precus8 = 0; $por8 = 0; $porcus8 = 0; $eng8 = 0; $engcus8 = 0; $oth8 = 0; $othcus8 = 0; $tolcus8 = 0;
        $wed9 = 0; $wedcus9 = 0; $pre9 = 0; $precus9 = 0; $por9 = 0; $porcus9 = 0; $eng9 = 0; $engcus9 = 0; $oth9 = 0; $othcus9 = 0; $tolcus9 = 0;
        $wed10 = 0; $wedcus10 = 0; $pre10 = 0; $precus10 = 0; $por10 = 0; $porcus10 = 0; $eng10 = 0; $engcus10 = 0; $oth10 = 0; $othcus10 = 0; $tolcus10 = 0;
        $wed11 = 0; $wedcus11 = 0; $pre11 = 0; $precus11 = 0; $por11 = 0; $porcus11 = 0; $eng11 = 0; $engcus11 = 0; $oth11 = 0; $othcus11 = 0; $tolcus11 = 0;
        $wed12 = 0; $wedcus12 = 0; $pre12 = 0; $precus12 = 0; $por12 = 0; $porcus12 = 0; $eng12 = 0; $engcus12 = 0; $oth12 = 0; $othcus12 = 0; $tolcus12 = 0;
        for ($i = 0, $il = count($result->data); $i < $il; $i++) {
          $customer = $result->data[$i];
          if ((SimpleDate::toStamp(SimpleDate::create($ye, 1, 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($ye, 1, 31)))) {
            $tolcus1++;
            for ($m = 0, $ml = count($customer->orders); $m < $ml; $m++) {
              $order = $customer->orders[$m];
              if ($order->disposal->status) {
                continue;
              }
              if ($order->type == ORDER_TYPE_WEDDING) {
                $wed1 += (float)$order->price;
                $wedcus1++;
              }
              if ($order->type == ORDER_TYPE_PREWEDDING) {
                $pre1 += (float)$order->price;
                $precus1++;
              }
              if ($order->type == ORDER_TYPE_PORTRAIT) {
                $por1 += (float)$order->price;
                $porcus1++;
              }
              if ($order->type == ORDER_TYPE_ENGAGEMENT) {
                $eng1 += (float)$order->price;
                $engcus1++;
              }
              if ($order->type == ORDER_TYPE_OTHER) {
                $oth1 += (float)$order->price;
                $othcus1++;
              }
            }
          }
          if ((SimpleDate::toStamp(SimpleDate::create($ye, 2, 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($ye, 2, 31)))) {
            $tolcus2++;
            for ($m = 0, $ml = count($customer->orders); $m < $ml; $m++) {
              $order = $customer->orders[$m];
              if ($order->disposal->status) {
                continue;
              }
              if ($order->type == ORDER_TYPE_WEDDING) {
                $wed2 += (float)$order->price;
                $wedcus2++;
              }
              if ($order->type == ORDER_TYPE_PREWEDDING) {
                $pre2 += (float)$order->price;
                $precus2++;
              }
              if ($order->type == ORDER_TYPE_PORTRAIT) {
                $por2 += (float)$order->price;
                $porcus2++;
              }
              if ($order->type == ORDER_TYPE_ENGAGEMENT) {
                $eng2 += (float)$order->price;
                $engcus2++;
              }
              if ($order->type == ORDER_TYPE_OTHER) {
                $oth2 += (float)$order->price;
                $othcus2++;
              }
            }
          }
          if ((SimpleDate::toStamp(SimpleDate::create($ye, 3, 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($ye, 3, 31)))) {
            $tolcus3++;
            for ($m = 0, $ml = count($customer->orders); $m < $ml; $m++) {
              $order = $customer->orders[$m];
              if ($order->disposal->status) {
                continue;
              }
              if ($order->type == ORDER_TYPE_WEDDING) {
                $wed3 += (float)$order->price;
                $wedcus3++;
              }
              if ($order->type == ORDER_TYPE_PREWEDDING) {
                $pre3 += (float)$order->price;
                $precus3++;
              }
              if ($order->type == ORDER_TYPE_PORTRAIT) {
                $por3 += (float)$order->price;
                $porcus3++;
              }
              if ($order->type == ORDER_TYPE_ENGAGEMENT) {
                $eng3 += (float)$order->price;
                $engcus3++;
              }
              if ($order->type == ORDER_TYPE_OTHER) {
                $oth3 += (float)$order->price;
                $othcus3++;
              }
            }
          }
          if ((SimpleDate::toStamp(SimpleDate::create($ye, 4, 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($ye, 4, 31)))) {
            $tolcus4++;
            for ($m = 0, $ml = count($customer->orders); $m < $ml; $m++) {
              $order = $customer->orders[$m];
              if ($order->disposal->status) {
                continue;
              }
              if ($order->type == ORDER_TYPE_WEDDING) {
                $wed4 += (float)$order->price;
                $wedcus4++;
              }
              if ($order->type == ORDER_TYPE_PREWEDDING) {
                $pre4 += (float)$order->price;
                $precus4++;
              }
              if ($order->type == ORDER_TYPE_PORTRAIT) {
                $por4 += (float)$order->price;
                $porcus4++;
              }
              if ($order->type == ORDER_TYPE_ENGAGEMENT) {
                $eng4 += (float)$order->price;
                $engcus4++;
              }
              if ($order->type == ORDER_TYPE_OTHER) {
                $oth4 += (float)$order->price;
                $othcus4++;
              }
            }
          }
          if ((SimpleDate::toStamp(SimpleDate::create($ye, 5, 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($ye, 5, 31)))) {
            $tolcus5++;
            for ($m = 0, $ml = count($customer->orders); $m < $ml; $m++) {
              $order = $customer->orders[$m];
              if ($order->disposal->status) {
                continue;
              }
              if ($order->type == ORDER_TYPE_WEDDING) {
                $wed5 += (float)$order->price;
                $wedcus5++;
              }
              if ($order->type == ORDER_TYPE_PREWEDDING) {
                $pre5 += (float)$order->price;
                $precus5++;
              }
              if ($order->type == ORDER_TYPE_PORTRAIT) {
                $por5 += (float)$order->price;
                $porcus5++;
              }
              if ($order->type == ORDER_TYPE_ENGAGEMENT) {
                $eng5 += (float)$order->price;
                $engcus5++;
              }
              if ($order->type == ORDER_TYPE_OTHER) {
                $oth5 += (float)$order->price;
                $othcus5++;
              }
            }
          }
          if ((SimpleDate::toStamp(SimpleDate::create($ye, 6, 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($ye, 6, 31)))) {
            $tolcus6++;
            for ($m = 0, $ml = count($customer->orders); $m < $ml; $m++) {
              $order = $customer->orders[$m];
              if ($order->disposal->status) {
                continue;
              }
              if ($order->type == ORDER_TYPE_WEDDING) {
                $wed6 += (float)$order->price;
                $wedcus6++;
              }
              if ($order->type == ORDER_TYPE_PREWEDDING) {
                $pre6 += (float)$order->price;
                $precus6++;
              }
              if ($order->type == ORDER_TYPE_PORTRAIT) {
                $por6 += (float)$order->price;
                $porcus6++;
              }
              if ($order->type == ORDER_TYPE_ENGAGEMENT) {
                $eng6 += (float)$order->price;
                $engcus6++;
              }
              if ($order->type == ORDER_TYPE_OTHER) {
                $oth6 += (float)$order->price;
                $othcus6++;
              }
            }
          }
          if ((SimpleDate::toStamp(SimpleDate::create($ye, 7, 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($ye, 7, 31)))) {
            $tolcus7++;
            for ($m = 0, $ml = count($customer->orders); $m < $ml; $m++) {
              $order = $customer->orders[$m];
              if ($order->disposal->status) {
                continue;
              }
              if ($order->type == ORDER_TYPE_WEDDING) {
                $wed7 += (float)$order->price;
                $wedcus7++;
              }
              if ($order->type == ORDER_TYPE_PREWEDDING) {
                $pre7 += (float)$order->price;
                $precus7++;
              }
              if ($order->type == ORDER_TYPE_PORTRAIT) {
                $por7 += (float)$order->price;
                $porcus7++;
              }
              if ($order->type == ORDER_TYPE_ENGAGEMENT) {
                $eng7 += (float)$order->price;
                $engcus7++;
              }
              if ($order->type == ORDER_TYPE_OTHER) {
                $oth7 += (float)$order->price;
                $othcus7++;
              }
            }
          }
          if ((SimpleDate::toStamp(SimpleDate::create($ye, 8, 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($ye, 8, 31)))) {
            $tolcus8++;
            for ($m = 0, $ml = count($customer->orders); $m < $ml; $m++) {
              $order = $customer->orders[$m];
              if ($order->disposal->status) {
                continue;
              }
              if ($order->type == ORDER_TYPE_WEDDING) {
                $wed8 += (float)$order->price;
                $wedcus8++;
              }
              if ($order->type == ORDER_TYPE_PREWEDDING) {
                $pre8 += (float)$order->price;
                $precus8++;
              }
              if ($order->type == ORDER_TYPE_PORTRAIT) {
                $por8 += (float)$order->price;
                $porcus8++;
              }
              if ($order->type == ORDER_TYPE_ENGAGEMENT) {
                $eng8 += (float)$order->price;
                $engcus8++;
              }
              if ($order->type == ORDER_TYPE_OTHER) {
                $oth8 += (float)$order->price;
                $othcus8++;
              }
            }
          }
          if ((SimpleDate::toStamp(SimpleDate::create($ye, 9, 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($ye, 9, 31)))) {
            $tolcus9++;
            for ($m = 0, $ml = count($customer->orders); $m < $ml; $m++) {
              $order = $customer->orders[$m];
              if ($order->disposal->status) {
                continue;
              }
              if ($order->type == ORDER_TYPE_WEDDING) {
                $wed9 += (float)$order->price;
                $wedcus9++;
              }
              if ($order->type == ORDER_TYPE_PREWEDDING) {
                $pre9 += (float)$order->price;
                $precus9++;
              }
              if ($order->type == ORDER_TYPE_PORTRAIT) {
                $por9 += (float)$order->price;
                $porcus9++;
              }
              if ($order->type == ORDER_TYPE_ENGAGEMENT) {
                $eng9 += (float)$order->price;
                $engcus9++;
              }
              if ($order->type == ORDER_TYPE_OTHER) {
                $oth9 += (float)$order->price;
                $othcus9++;
              }
            }
          }
          if ((SimpleDate::toStamp(SimpleDate::create($ye, 10, 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($ye, 10, 31)))) {
            $tolcus10++;
            for ($m = 0, $ml = count($customer->orders); $m < $ml; $m++) {
              $order = $customer->orders[$m];
              if ($order->disposal->status) {
                continue;
              }
              if ($order->type == ORDER_TYPE_WEDDING) {
                $wed10 += (float)$order->price;
                $wedcus10++;
              }
              if ($order->type == ORDER_TYPE_PREWEDDING) {
                $pre10 += (float)$order->price;
                $precus10++;
              }
              if ($order->type == ORDER_TYPE_PORTRAIT) {
                $por10 += (float)$order->price;
                $porcus10++;
              }
              if ($order->type == ORDER_TYPE_ENGAGEMENT) {
                $eng10 += (float)$order->price;
                $engcus10++;
              }
              if ($order->type == ORDER_TYPE_OTHER) {
                $oth10 += (float)$order->price;
                $othcus10++;
              }
            }
          }
          if ((SimpleDate::toStamp(SimpleDate::create($ye, 11, 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($ye, 11, 31)))) {
            $tolcus11++;
            for ($m = 0, $ml = count($customer->orders); $m < $ml; $m++) {
              $order = $customer->orders[$m];
              if ($order->disposal->status) {
                continue;
              }
              if ($order->type == ORDER_TYPE_WEDDING) {
                $wed11 += (float)$order->price;
                $wedcus11++;
              }
              if ($order->type == ORDER_TYPE_PREWEDDING) {
                $pre11 += (float)$order->price;
                $precus11++;
              }
              if ($order->type == ORDER_TYPE_PORTRAIT) {
                $por11 += (float)$order->price;
                $porcus11++;
              }
              if ($order->type == ORDER_TYPE_ENGAGEMENT) {
                $eng11 += (float)$order->price;
                $engcus11++;
              }
              if ($order->type == ORDER_TYPE_OTHER) {
                $oth11 += (float)$order->price;
                $othcus11++;
              }
            }
          }
          if ((SimpleDate::toStamp(SimpleDate::create($ye, 12, 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($ye, 12, 31)))) {
            $tolcus12++;
            for ($m = 0, $ml = count($customer->orders); $m < $ml; $m++) {
              $order = $customer->orders[$m];
              if ($order->disposal->status) {
                continue;
              }
              if ($order->type == ORDER_TYPE_WEDDING) {
                $wed12 += (float)$order->price;
                $wedcus12++;
              }
              if ($order->type == ORDER_TYPE_PREWEDDING) {
                $pre12 += (float)$order->price;
                $precus12++;
              }
              if ($order->type == ORDER_TYPE_PORTRAIT) {
                $por12 += (float)$order->price;
                $porcus12++;
              }
              if ($order->type == ORDER_TYPE_ENGAGEMENT) {
                $eng12 += (float)$order->price;
                $engcus12++;
              }
              if ($order->type == ORDER_TYPE_OTHER) {
                $oth12 += (float)$order->price;
                $othcus12++;
              }
            }
          }
        }
        $ob1 = new stdClass();
        $ob1->year = $ye;
        $ob1->total += $wed1 + $pre1 + $por1 + $eng1 + $oth1;
        $ob1->tolcus = $tolcus1;
        $ob1->wed = $wed1;
        $ob1->wedcus = $wedcus1;
        $ob1->pre = $pre1;
        $ob1->precus = $precus1;
        $ob1->por = $por1;
        $ob1->porcus = $porcus1;
        $ob1->eng = $eng1;
        $ob1->engcus = $engcus1;
        $ob1->oth = $oth1;
        $ob1->othcus = $othcus1;
        
        $ob2 = new stdClass();
        $ob2->year = $ye;
        $ob2->total += $wed2 + $pre2 + $por2 + $eng2 + $oth2;
        $ob2->tolcus = $tolcus2;
        $ob2->wed = $wed2;
        $ob2->wedcus = $wedcus2;
        $ob2->pre = $pre2;
        $ob2->precus = $precus2;
        $ob2->por = $por2;
        $ob2->porcus = $porcus2;
        $ob2->eng = $eng2;
        $ob2->engcus = $engcus2;
        $ob2->oth = $oth2;
        $ob2->othcus = $othcus2;
        
        $ob3 = new stdClass();
        $ob3->year = $ye;
        $ob3->total += $wed3 + $pre3 + $por3 + $eng3 + $oth3;
        $ob3->tolcus = $tolcus3;
        $ob3->wed = $wed3;
        $ob3->wedcus = $wedcus3;
        $ob3->pre = $pre3;
        $ob3->precus = $precus3;
        $ob3->por = $por3;
        $ob3->porcus = $porcus3;
        $ob3->eng = $eng3;
        $ob3->engcus = $engcus3;
        $ob3->oth = $oth3;
        $ob3->othcus = $othcus3;
        
        $ob4 = new stdClass();
        $ob4->year = $ye;
        $ob4->total += $wed4 + $pre4 + $por4 + $eng4 + $oth4;
        $ob4->tolcus = $tolcus4;
        $ob4->wed = $wed4;
        $ob4->wedcus = $wedcus4;
        $ob4->pre = $pre4;
        $ob4->precus = $precus4;
        $ob4->por = $por4;
        $ob4->porcus = $porcus4;
        $ob4->eng = $eng4;
        $ob4->engcus = $engcus4;
        $ob4->oth = $oth4;
        $ob4->othcus = $othcus4;
        
        $ob5 = new stdClass();
        $ob5->year = $ye;
        $ob5->total += $wed5 + $pre5 + $por5 + $eng5 + $oth5;
        $ob5->tolcus = $tolcus5;
        $ob5->wed = $wed5;
        $ob5->wedcus = $wedcus5;
        $ob5->pre = $pre5;
        $ob5->precus = $precus5;
        $ob5->por = $por5;
        $ob5->porcus = $porcus5;
        $ob5->eng = $eng5;
        $ob5->engcus = $engcus5;
        $ob5->oth = $oth5;
        $ob5->othcus = $othcus5;
        
        $ob6 = new stdClass();
        $ob6->year = $ye;
        $ob6->total += $wed6 + $pre6 + $por6 + $eng6 + $oth6;
        $ob6->tolcus = $tolcus6;
        $ob6->wed = $wed6;
        $ob6->wedcus = $wedcus6;
        $ob6->pre = $pre6;
        $ob6->precus = $precus6;
        $ob6->por = $por6;
        $ob6->porcus = $porcus6;
        $ob6->eng = $eng6;
        $ob6->engcus = $engcus6;
        $ob6->oth = $oth6;
        $ob6->othcus = $othcus6;
        
        $ob7 = new stdClass();
        $ob7->year = $ye;
        $ob7->total += $wed7 + $pre7 + $por7 + $eng7 + $oth7;
        $ob7->tolcus = $tolcus7;
        $ob7->wed = $wed7;
        $ob7->wedcus = $wedcus7;
        $ob7->pre = $pre7;
        $ob7->precus = $precus7;
        $ob7->por = $por7;
        $ob7->porcus = $porcus7;
        $ob7->eng = $eng7;
        $ob7->engcus = $engcus7;
        $ob7->oth = $oth7;
        $ob7->othcus = $othcus7;
        
        $ob8 = new stdClass();
        $ob8->year = $ye;
        $ob8->total += $wed8 + $pre8 + $por8 + $eng8 + $oth8;
        $ob8->tolcus = $tolcus8;
        $ob8->wed = $wed8;
        $ob8->wedcus = $wedcus8;
        $ob8->pre = $pre8;
        $ob8->precus = $precus8;
        $ob8->por = $por8;
        $ob8->porcus = $porcus8;
        $ob8->eng = $eng8;
        $ob8->engcus = $engcus8;
        $ob8->oth = $oth8;
        $ob8->othcus = $othcus8;
        
        $ob9 = new stdClass();
        $ob9->year = $ye;
        $ob9->total += $wed9 + $pre9 + $por9 + $eng9 + $oth9;
        $ob9->tolcus = $tolcus9;
        $ob9->wed = $wed9;
        $ob9->wedcus = $wedcus9;
        $ob9->pre = $pre9;
        $ob9->precus = $precus9;
        $ob9->por = $por9;
        $ob9->porcus = $porcus9;
        $ob9->eng = $eng9;
        $ob9->engcus = $engcus9;
        $ob9->oth = $oth9;
        $ob9->othcus = $othcus9;
        
        $ob10 = new stdClass();
        $ob10->year = $ye;
        $ob10->total += $wed10 + $pre10 + $por10 + $eng10 + $oth10;
        $ob10->tolcus = $tolcus10;
        $ob10->wed = $wed10;
        $ob10->wedcus = $wedcus10;
        $ob10->pre = $pre10;
        $ob10->precus = $precus10;
        $ob10->por = $por10;
        $ob10->porcus = $porcus10;
        $ob10->eng = $eng10;
        $ob10->engcus = $engcus10;
        $ob10->oth = $oth10;
        $ob10->othcus = $othcus10;
        
        $ob11 = new stdClass();
        $ob11->year = $ye;
        $ob11->total += $wed11 + $pre11 + $por11 + $eng11 + $oth11;
        $ob11->tolcus = $tolcus11;
        $ob11->wed = $wed11;
        $ob11->wedcus = $wedcus11;
        $ob11->pre = $pre11;
        $ob11->precus = $precus11;
        $ob11->por = $por11;
        $ob11->porcus = $porcus11;
        $ob11->eng = $eng11;
        $ob11->engcus = $engcus11;
        $ob11->oth = $oth11;
        $ob11->othcus = $othcus11;
        
        $ob12 = new stdClass();
        $ob12->year = $ye;
        $ob12->total += $wed12 + $pre12 + $por12 + $eng12 + $oth12;
        $ob12->tolcus = $tolcus12;
        $ob12->wed = $wed12;
        $ob12->wedcus = $wedcus12;
        $ob12->pre = $pre12;
        $ob12->precus = $precus12;
        $ob12->por = $por12;
        $ob12->porcus = $porcus12;
        $ob12->eng = $eng12;
        $ob12->engcus = $engcus12;
        $ob12->oth = $oth12;
        $ob12->othcus = $othcus12;
        array_push($a1, $ob1);
        array_push($a2, $ob2);
        array_push($a3, $ob3);
        array_push($a4, $ob4);
        array_push($a5, $ob5);
        array_push($a6, $ob6);
        array_push($a7, $ob7);
        array_push($a8, $ob8);
        array_push($a9, $ob9);
        array_push($a10, $ob10);
        array_push($a11, $ob11);
        array_push($a12, $ob12);
      }
      $m1 = new stdClass();$m2 = new stdClass();$m3 = new stdClass();$m4 = new stdClass();$m5 = new stdClass();$m6 = new stdClass();
      $m7 = new stdClass();$m8 = new stdClass();$m9 = new stdClass();$m10 = new stdClass();$m11 = new stdClass();$m12 = new stdClass();
      $m1->month = 1;
      $m1->mains = $a1;
      $m2->month = 2;
      $m2->mains = $a2;
      $m3->month = 3;
      $m3->mains = $a3;
      $m4->month = 4;
      $m4->mains = $a4;
      $m5->month = 5;
      $m5->mains = $a5;
      $m6->month = 6;
      $m6->mains = $a6;
      $m7->month = 7;
      $m7->mains = $a7;
      $m8->month = 8;
      $m8->mains = $a8;
      $m9->month = 9;
      $m9->mains = $a9;
      $m10->month = 10;
      $m10->mains = $a10;
      $m11->month = 11;
      $m11->mains = $a11;
      $m12->month = 12;
      $m12->mains = $a12;
      array_push($res, $m1, $m2, $m3, $m4, $m5, $m6, $m7, $m8, $m9, $m10, $m11, $m12);
      echo json_encode($res);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function iecompbyyear($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', 'd.createdDate');
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);

  $condition .= ($createdFrom) ? ' AND (d.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $orm = classToOrm('customer');
  if ($orm) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      $year = array();
      for ($i = 0, $il = count($result->data); $i < $il; $i++) {
        $customer = $result->data[$i];
        $exist = false;
        for ($j = 0, $jl = count($year); $j < $jl; $j++) {
          $y = $year[$j];
          if ($y == $customer->createdDate->year) {
            $exist = true;
          }
        }
        if (!$exist) {
          array_push($year, $customer->createdDate->year);
        }
      }
      $res = array();
      for ($k = 0, $kl = count($year); $k < $kl; $k++) {
        $ye = $year[$k];
        $wed = 0; $wedcus = 0; $pre = 0; $precus = 0; $por = 0; $porcus = 0; $eng = 0; $engcus = 0; $oth = 0; $othcus = 0; $total = 0; $tolcus = 0;
        for ($i = 0, $il = count($result->data); $i < $il; $i++) {
          $customer = $result->data[$i];
          if ((SimpleDate::toStamp(SimpleDate::create($ye, 1, 1)) <= SimpleDate::toStamp($customer->createdDate)) && (SimpleDate::toStamp($customer->createdDate) <= SimpleDate::toStamp(SimpleDate::create($ye, 12, 31)))) {
            $tolcus++;
            for ($m = 0, $ml = count($customer->orders); $m < $ml; $m++) {
              $order = $customer->orders[$m];
              if ($order->disposal->status) {
                continue;
              }
              if ($order->type == ORDER_TYPE_WEDDING) {
                $wed += (float)$order->price;
                $wedcus++;
              }
              if ($order->type == ORDER_TYPE_PREWEDDING) {
                $pre += (float)$order->price;
                $precus++;
              }
              if ($order->type == ORDER_TYPE_PORTRAIT) {
                $por += (float)$order->price;
                $porcus++;
              }
              if ($order->type == ORDER_TYPE_ENGAGEMENT) {
                $eng += (float)$order->price;
                $engcus++;
              }
              if ($order->type == ORDER_TYPE_OTHER) {
                $oth += (float)$order->price;
                $othcus++;
              }
            }
          }
        }
        $ob= new stdClass();
        $ob->year = $ye;
        $ob->total += $wed + $pre + $por + $eng + $oth;
        $ob->tolcus = $tolcus;
        $ob->wed = $wed;
        $ob->wedcus = $wedcus;
        $ob->pre = $pre;
        $ob->precus = $precus;
        $ob->por = $por;
        $ob->porcus = $porcus;
        $ob->eng = $eng;
        $ob->engcus = $engcus;
        $ob->oth = $oth;
        $ob->othcus = $othcus;
        array_push($res, $ob);
      }
      echo json_encode($res);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function iesumexpensedetail($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $condition .= ($createdFrom) ? ' AND (d.payDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.payDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $orm = classToOrm('expense');
  if ($orm) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      $result1 = $orm->find($myManager, START, INFINITE, $order, $queue, $condition, $function);
      
      $topResult = array(); $mainResult = array();
      for ($i = 0, $il = count($result1->data); $i < $il; $i++) {
        $exp = $result1->data[$i];
        $exist = false;
        for ($j = 0, $jl = count($topResult); $j < $jl; $j++) {
          $t = $topResult[$j];
          if ($t->payee == $exp->payee) {
            $t->total += $exp->amount;
            if ($exp->method == 'C') {
              $t->cash += $exp->amount;
            } else {
              $t->noncash += $exp->amount;
            }
            $exist = true;
          }
        }
        if (!$exist) {
          $obj = new stdClass();
          $obj->payee = $exp->payee;
          $obj->total = $exp->amount;
          $obj->cash = ($exp->method == 'C') ? $exp->amount : 0;
          $obj->noncash = ($exp->method == 'C') ? 0 : $exp->amount;
          array_push($topResult, $obj);
        }
      }
      $res = new stdClass();
      $res->top = $topResult;
      echo json_encode($res);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}
function isLogin($myManager) {
  $res = array();
  if (isset($_SESSION[OPERATOR])) {
    if ($_SESSION[OPERATOR]->type == USER_TYPE_MANAGER_ASSISTANT || $_SESSION[OPERATOR]->type == USER_TYPE_CUSTOMER_SERVICE) {
      array_push($res, $_SESSION[OPERATOR]);
    }
  }
  echo json_encode($res);
}

function acctexpenses($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', 'd.payDate');
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  $payFrom = MiscUtils::getParam('pfrom', NULL);
  $payTo = MiscUtils::getParam('pto', NULL);

  $condition .= ($createdFrom) ? ' AND (d.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  $condition .= ($payFrom) ? ' AND (d.payDate >= \'' . SimpleDate::toStamp(json_decode($payFrom)) . '\')' : '';
  $condition .= ($payTo) ? ' AND (d.payDate <= \'' . SimpleDate::toStamp(json_decode($payTo)) . '\')' : '';

  $orm = classToOrm('expense');
  if ($orm) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      echo json_encode($result);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function acctexpense($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  $payFrom = MiscUtils::getParam('pfrom', NULL);
  $payTo = MiscUtils::getParam('pto', NULL);

  $condition .= ($createdFrom) ? ' AND (d.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  $condition .= ($payFrom) ? ' AND (d.payDate >= \'' . SimpleDate::toStamp(json_decode($payFrom)) . '\')' : '';
  $condition .= ($payTo) ? ' AND (d.payDate <= \'' . SimpleDate::toStamp(json_decode($payTo)) . '\')' : '';
  
  $orm = classToOrm('expense');
  if ($orm) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      $exp = 0; $cash = 0; $noncash = 0;

      for ($i = 0, $il = count($result->data); $i < $il; $i++) {
        $expense = $result->data[$i];
        if ($expense->status == -1) {
          continue;
        }
        $exp += $expense->amount;
        if ($expense->method == 'C') {
          $cash += $expense->amount;
        } else {
          $noncash += $expense->amount;
        }
      }
      $res = new stdClass();
      $res->expense = $exp;
      $res->cash = $cash;
      $res->noncash = $noncash;

      echo json_encode($res);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function acctpayments($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', 'd.date');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  $payFrom = MiscUtils::getParam('pfrom', NULL);
  $payTo = MiscUtils::getParam('pto', NULL);
  
  $condition .= ($createdFrom) ? ' AND (d.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  $condition .= ($payFrom) ? ' AND (d.date >= \'' . SimpleDate::toStamp(json_decode($payFrom)) . '\')' : '';
  $condition .= ($payTo) ? ' AND (d.date <= \'' . SimpleDate::toStamp(json_decode($payTo)) . '\')' : '';
  
  $orm = classToOrm('payment');
  if ($orm) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      echo json_encode($result);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function acctpayment($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  $payFrom = MiscUtils::getParam('pfrom', NULL);
  $payTo = MiscUtils::getParam('pto', NULL);
  
  $condition .= ($createdFrom) ? ' AND (d.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  $condition .= ($payFrom) ? ' AND (d.date >= \'' . SimpleDate::toStamp(json_decode($payFrom)) . '\')' : '';
  $condition .= ($payTo) ? ' AND (d.date <= \'' . SimpleDate::toStamp(json_decode($payTo)) . '\')' : '';
  
  $orm = classToOrm('payment');
  if ($orm) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      $receipt = 0; $cash = 0; $noncash = 0;
      for ($i = 0, $il = count($result->data); $i < $il; $i++) {
        $payment = $result->data[$i];
        if ($payment->cancelled) {
          continue;
        }
        $receipt += $payment->amount;
        if ($payment->method == 'cash') {
          $cash += $payment->amount;
        } else {
          $noncash += $payment->amount;
        }
      }
      $top = new stdClass();
      $top->receipt = $receipt;
      $top->cash = $cash;
      $top->noncash = $noncash;
      
      $res = new stdClass();
      $res->top = $top;
      echo json_encode($res);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function asstwages($myManager) {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', 'd.payDate');
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);
  
  $date = MiscUtils::getParam('date', NULL);
  
  $condition .= ($date) ? ' AND (d.payDate = \'' . SimpleDate::toStamp(json_decode($date)) . '\')' : '';

  $orm = classToOrm('expense');
  if ($orm) {
    try {
      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
      echo json_encode($result);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function weeklywage($myManager) {
  
  $date = json_decode(MiscUtils::getParam('date', NULL));
  $wages = json_decode(MiscUtils::getParam('exp', NULL));

  $orm = classToOrm('expense');
  $orm1 = classToOrm('wagedate');
  if ($orm && $orm1) {
    try {
      $epDate = $orm1->add($myManager, $date);
      $myManager->commit_t();
      $myManager->start_t();
      $wagedate = $orm1->__toObject($myManager, $epDate, new stdClass());
      foreach($wages as &$wage) {
        $wage->weeklyWageId = $wagedate->id;
        $wage->status = 1;
        $orm->add($myManager, $wage);
      }
      $myManager->commit_t();
      $myManager->start_t();
      echo json_encode(array());
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function weeklywage1($myManager) {
  
  $date = json_decode(MiscUtils::getParam('date', NULL));
  $wages = json_decode(MiscUtils::getParam('exp', NULL));

  $orm = classToOrm('expense');
  $orm1 = classToOrm('wagedate');
  if ($orm && $orm1) {
    try {
      $orm1->update($myManager, $date);
      foreach($wages as &$wage) {
        $orm->update($myManager, $wage);
      }
      $myManager->commit_t();
      $myManager->start_t();

      echo json_encode(array());
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function purchasesample($myManager) {
  $sample = json_decode(MiscUtils::getParam('sp', NULL));
  $purchases = json_decode(MiscUtils::getParam('pi', NULL));

  $orm = classToOrm('sample');
  $orm1 = classToOrm('purchaseitem');
  if ($orm && $orm1) {
    try {
      foreach($purchases as &$p) {
        $orm1->update($myManager, $p);
      }
      $orm->update($myManager, $sample);
      
      $myManager->commit_t();
      $myManager->start_t();
      echo json_encode(array());
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function packagesample($myManager) {
  $sample = json_decode(MiscUtils::getParam('sp', NULL));
  $packages = json_decode(MiscUtils::getParam('pi', NULL));

  $orm = classToOrm('sample');
  $orm1 = classToOrm('packageitem');
  
  if ($orm && $orm1) {
    try {
      
      foreach($packages as &$p) {
        $orm1->update($myManager, $p);
      }
      
      $orm->update($myManager, $sample);
      
      $myManager->commit_t();
      $myManager->start_t();
      echo json_encode(array());
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function packagepurchase($myManager) {
  $sample = json_decode(MiscUtils::getParam('sp', NULL));
  $packages = json_decode(MiscUtils::getParam('pi', NULL));
  $purchases = json_decode(MiscUtils::getParam('ps', NULL));

  $orm = classToOrm('sample');
  $orm1 = classToOrm('packageitem');
  $orm2 = classToOrm('purchaseitem');
  
  if ($orm && $orm1 && $orm2) {
    try {
      
      foreach($packages as &$p) {
        $orm1->update($myManager, $p);
      }
      
      foreach($purchases as &$p) {
        $orm2->update($myManager, $p);
      }
      
      $orm->update($myManager, $sample);
      
      $myManager->commit_t();
      $myManager->start_t();
      echo json_encode(array());
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

//function produces($myManager) {
//  $function = MiscUtils::getParam('f', NULL);
//  $condition = MiscUtils::getParam('c', NULL);
//  $order = MiscUtils::getParam('o', 'd.deadline');
//  $queue = MiscUtils::getParam('q', NULL);
//  $page = MiscUtils::getParam('p', NULL);
//  $size = MiscUtils::getParam('s', NULL);
//  
//  $createdFrom = MiscUtils::getParam('from', NULL);
//  $createdTo = MiscUtils::getParam('to', NULL);
//  $payFrom = MiscUtils::getParam('pfrom', NULL);
//  $payTo = MiscUtils::getParam('pto', NULL);
//
//  $condition .= ($createdFrom) ? ' AND (d.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
//  $condition .= ($createdTo) ? ' AND (d.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
//  $condition .= ($payFrom) ? ' AND (d.payDate >= \'' . SimpleDate::toStamp(json_decode($payFrom)) . '\')' : '';
//  $condition .= ($payTo) ? ' AND (d.payDate <= \'' . SimpleDate::toStamp(json_decode($payTo)) . '\')' : '';
//
//  $orm = classToOrm('expense');
//  if ($orm) {
//    try {
//      $result = $orm->find($myManager, $page, $size, $order, $queue, $condition, $function);
//      $result->total = count($orm->find($myManager, START, INFINITE, NULL, NULL, $condition, NULL)->data);
//      echo json_encode($result);
//    } catch (PDOException $e) {
//      echo $e->getMessage();
//    }
//  }
//}
?>